Skip to content Skip to sidebar Skip to footer

Open Info Activity Without Closing Main Activity

I'm making a simple android application with two activities. one MainActivity and one InfoActivity. I have a button on my ActionBar and on click I want to open my InfoActivity. In

Solution 1:

Can I open the InfoActivity without closing my MainActivity?

No, the InfoActivity will be called to the foreground and MainActivity in the background, because it will be in the state stopped. The problem is if you fire the Intent in your InfoActivity like this, there will be a new instance of the MainActivity besides the old instance.

Add a flag to your Intent call to prevent a new instance of the MainActivity. Then it will be called from the stack.

private View.OnClickListeneronBackBtnClick=newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        Intentintent=newIntent(InfoActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
};

Or simple call finish(), like hamad and Aerrow suggested. Then the InfoActivity will be destroyed and the MainActivity will be called from the stack.

Solution 2:

start info activity using

Intent i = newIntent(MainActivity.this, InfoActivity.class);
startActivityForResult(i, 1);

and in infoactivity just call finish() on button click do not start activity again.then you will get back to previous activity

Post a Comment for "Open Info Activity Without Closing Main Activity"