startActivityForResult vs OnRestart(包或从文件中读取)

时间:2014-12-03 06:59:38

标签: java android bundle start-activity onstart

主要Java活动:

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();
    }
}

}

添加Java活动:

// 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();
}

主要活动在重启时首先做什么?它是否执行预期的操作并从文件中读取tl并从包中添加myCostumObj?如果是这样,那么我做错了,因为tl总是显得空洞。

0 个答案:

没有答案