简体   繁体   中英

Activity getting destroyed while launching new activity

Intent showImage = new Intent(MainActivity.this,ImageViewerActivity.class);
startActivity(showImage);

why is MainActivity is getting destroyed (onDestroy() called) , while launching a new activity?

Your activity is destroyed either because system is running out of space or because you called finish() on MainActivity somewhere.

You can read the docs here: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Your activity is getting destroy because you create a new context in your invocation , you should do this:

Intent showImage = new Intent(this,ImageViewerActivity.class); 
startActivity(showImage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM