startActivityForResult vs OnRestart (bundle or read from file)

111 views Asked by At

Main Java Activity:

TList tl = new TList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
}

public void AddmyCostumObj_buttonclik(View view) {

    Intent intent = new Intent(this, AddActivity.class);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK){
        Bundle T = data.getExtras();
        String new_myCostumObj_s = myCostumObj.myCostumObj_getString("ret");
        myCostumObj new1 = new myCostumObj();
        new1.string_to_myCostumObj(new_myCostumObj_s);
        tl.add_myCostumObj(new1);
    }
}

@Override
protected void onStart() {
    super.onStart();

    FileInputStream fis;
    try {
        fis = openFileInput("data_tasks");

        ObjectInputStream ois = new ObjectInputStream(fis);
        tl = (TList) ois.readObject();
        if (tl == null) tl = new TList(); // it sometimes gave nullPointer

        ois.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();

    FileOutputStream fos;
    try {
        fos = openFileOutput("data_tasks", Context.MODE_PRIVATE);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tl);
        oos.flush();
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}

}

Add Java Activity:

// creates a new myCostumObj
public void add_buttonclick(View view){

    Intent intent = new Intent();
    Bundle T = new Bundle();
    T.putString("ret", myCostumObj.myCostumObj_toString());
    intent.putExtras(T);
    setResult(RESULT_OK, intent);
    finish();
}

What does the main activity do first on restart? Does it do the expected thing and read form the file the tl and than add the myCostumObj from the bundle? If so, than what am I doing wrong, because the tl is always appearing empty.

0

There are 0 answers