How to safely exit onResume () method?

My onResume () activity reads some “extra” data from the intent that triggered it, and updates the interface accordingly.

I would like to add error handling: if the data in the Intent is missing / corrupted, the activity displays Toast and ends.

Is it possible to just call finish () in the onResume () method? I worry about some unexpected things, given that both of them are related to the life cycle.

If there are other better ways, I am also interested in them, but the above seems simple.

Thank!

+5
source share
3 answers

, finish() - .

, , - / finish(), .

+4

finish() onResume() . onResume(), onCreate()?

0

finish() onActivityResult(), onResume() onPostResume(). Nexus 7 Android Android 4.4.2.

, , finish() , Runnable:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}
0

All Articles