How to keep an activity alive while closing all the others in android?

心不动则不痛 提交于 2019-12-25 17:47:29

问题


I have a main activity that contains a drawer and then i have other activities that opens on top of it they also have the drawer menu(they inherit main activity)

public class OBDActivity  extends MainActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.activity_setting, null, false);
    mDrawer.addView(contentView, 0);
    getSupportActionBar().setTitle(R.string.OBDParams);
 }
 ...
}

I want to keep the main activity alive and kill all of the other activities when a new activity is being opened?

I already tried this but it just kill the old activity that is the same activity that is being opened.

Intent intent=new Intent(this, MessagesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

In fact I want to start new Activity and finish current one either its the same activity or a different type.


回答1:


Try This

Intent intent = new Intent(ActivityC.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

For More about intent




回答2:


The only way i could do this was to handle closing old activity by myself so i kept a reference of the activity that was opening and close it when a new one was going to open.

I created a variable named oldActivity in my Main Activity and i set it in each and every other activities onCreate method :

 openedActivity = this;

Then whenever another activity was going to open from drawer menu i just finished the old one:

  oldActivity.finish();



回答3:


No, You cannot do this using common Flags of activities or any function.Not certainly of my knowledge

But there is one workaround for it , you can clear all activities from backstack and then reopen the first one.

Try This:

Intent intents = new Intent(CurrentActivity.this, FirstActivity.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TOP
            | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();



回答4:


You need to use your MainActivity as a "dispatcher". When you want to open MessagesActivity, do this:

Intent intent=new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Add an extra that specifies the name of the class that should be started by MainActivity
intent.putExtra("startActivity", MessagesActivity.class.getName());
startActivity(intent);

This will clear the activity stack all the way back to your existing instance of MainActivity.

Now, in MainActivity, override onNewIntent() and take care of dispatching to the desired Activity:

@Override
protected void onNewIntent(Intent intent) {
    // Check if we should launch another Activity
    if (intent.hasExtra("startActivity") {
        // Start the desired Activity
        String activityName = intent.getStringExtra("startActivity");
        Intent dispatchIntent = new Intent(this, Class.forName(activityName));
        startActivity(dispatchIntent);
    }
}

This will launch the desired Activity on top of MainActivity.



来源:https://stackoverflow.com/questions/46403448/how-to-keep-an-activity-alive-while-closing-all-the-others-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!