关闭所有正在运行的活动Android应用程序?应用程序、正在运行、Android

2023-09-12 22:13:30 作者:少不顺眼。

我创建一个应用程序,并从来没有使用结束()每项活动。如果我的用户点击注销按钮,它进入到previous页面。

I create one application and never use finish() for each activity. If my user clicks on the logout button it goes to the previous page.

如何关闭我的previous活动并关闭应用程序?

How can I close my previous activity and close the application?

推荐答案

这是我已经尝试过一种解决方法,它的工作对我来说非常完美。

This is one workaround that i have tried and it worked for me perfectly.

解决方案-1

this.finish();//try activityname.finish instead of this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

在那里,你想从你的应用程序退出的活动

使用这个....

use this in the activity where you want to exit from your application....

=============================================== =================================== 以上code有助于恢复您的应用程序,你上次离开。

================================================================================== Above code helps to resume your app where you last left off.

解决方案-2

如果您想从应用程序退出,并关闭你需要使用所有正在运行的活动。

If you want to exit from the application and also close all running activities you need to use.

onActivityResult()

有关EG-假设有3个活动A,B,C,你从A-> B-导航> C和位于C要关闭所有的活动,然后使用下面的示例。

For eg- suppose there are 3 activities A,B,C you navigate from A->B->C and at C you want to close all activities then use following sample.

活动A

public class A extends Activity {
int Finish = 100;
Button launch_second;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    launch_second = (Button) findViewById(R.id.start_second_act);
    launch_second.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent second = new Intent(A.this,
                    B.class);
            startActivityForResult(second, Finish);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        this.finish();
        break;
    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);

}

b活动

public class B extends Activity {
private Button launch_next;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_main);
    launch_next = (Button) findViewById(R.id.start_third_activity);
    launch_next.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent third = new Intent(B.this,C.class);
            startActivityForResult(third, 100);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        setResult(requestCode);
        this.finish();
        break;

    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

活动ç

public class C extends Activity {
Button kill_app;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third_main);
    kill_app = (Button)findViewById(R.id.kill_app_btn);
    kill_app.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            C.this.finish();
            setResult(100);

        }
    });

}

}
 
精彩推荐
图片推荐