Unable to close all activities on android eclipse

后端 未结 4 1965
眼角桃花
眼角桃花 2020-12-06 09:06

Assume i have four classes.Each class is having a button to do screen switching from one page to another page,well its working fine.But now im trying to close all the activi

相关标签:
4条回答
  • 2020-12-06 09:24

    Simply try this. Consider your Class3.java is your last Activitiy And, in Button of next use below code -

    Intent intent = new Intent(currentActivity.this, XitActivity.class); // instead of XitActivity use your first activity
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);
    

    And, in your Xit_demoActivity's onCreate() use below code -

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }
    

    Hope this will helps you to exit you from all activities.

    0 讨论(0)
  • 2020-12-06 09:27

    I would insist to create an BaseActivity with a BroadCastReceiver that will notify when you want to finish all the Activities and extend all the Activity classes with this BaseActivity,

    public static final String ACTION_FINISH = "ACTION_FINISH";
    public class BaseActivity extends Activity {
    
    public void onCreate(Bundle bundle, int resourceId) {
        super.onCreate(bundle);
        registerReceiver(finishActivitiesReceiver, 
                                              new IntentFilter(ACTION_FINISH));
    }
    
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(finishActivitiesReceiver);
    }
    
    BroadcastReceiver finishActivitiesReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
            }
        };
    }
    

    Then you can just send BroadCast when you want to close all Activities using

    sendBroadcast(new Intent(BaseActivity.ACTION_FINISH));
    
    0 讨论(0)
  • 2020-12-06 09:27

    You should not do this. If you want to open home screen or previous app screen. You can simply call

    moveTaskToBack(boolean bool);
    

    it will send your app to background. or if you want to not have class1 2 and 3 in stack you can use excludeFromRecent flag in manifest decl's of activities.

    This thread got thousands of views, for the same issue. have a look on it and make a understanding.

    Is quitting an application frowned upon?

    0 讨论(0)
  • 2020-12-06 09:36

    On the click of button in Class 3 write this:

    Intent intent=new Intent(Class3.this,Class1.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题