1

I have a problem concerning startActivity(intent) and onStop. The Android API suggests to save data in onStop, which is what I am doing here:

public void onStop(){
    super.onStop();

    if(tosave)
    {
        Editor editor = sp.edit();
        editor.putInt(getString(R.string.index_of_text_color), text_color_index);
        editor.putInt(getString(R.string.index_of_background_color), background_color_index);
        editor.commit();
    }
}

However, I would like to start the next activity once it is saved- so I need to use an intent and startActivity(intent).

public void click(View v){
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

My question is, does startActivity(intent) with the two flags call the onStop() method as it is finishing the application, or do I need to call finish()? Is it allowed for me to call finish() after I start a new activity? Or, is it because the new activity will be at the top, Android automatically calls onStop as it is in the background now?

There are so many questions about how startActivity(intent) works with the app cycle that I do not understand. It will be helpful if someone points me to a link.

Thank you!

EDIT: the intent is not in onStop. It is in another method which corresponds to a button. As soon as the button is clicked, I will need to save the data-- go to onstop-- and then go to the next activity. How can I do that?

4

1 回答 1

0

您应该将状态保存在onPause(), not中onStop(),因为在某些情况下onStop()永远不会被调用。你保证onPause()会被调用。

onPause()当另一个活动显示在它前面时,将始终在您的活动上调用。

那应该可以解决您的问题。

于 2013-11-12T20:37:03.757 回答