关闭应用程序并在 Android 上启动主屏幕

我有两个不同的活动。第一个发射第二个。在第二个活动中,我调用 System.exit(0)以强制关闭应用程序,但是第一个活动会自动显示,而不是应用程序返回主屏幕。如何避免这种情况,并使应用程序返回到主屏幕?

159267 次浏览

When you launch the second activity, finish() the first one immediately:

startActivity(new Intent(...));
finish();

You can also specify noHistory = "true" in the tag for first activity or finish the first activity as soon as you start the second one(as David said).

AFAIK, "force close" kills the process which hosts the JVM in which your application runs and System.exit() terminates the JVM running your application instance. Both are form of abrupt terminations and not advisable for normal application flow.

Just as catching exceptions to cover logic flows that a program might undertake, is not advisable.

You can not do System.exit(), it's not safe.

You can do this one: Process.killProcess(Process.myPid());

Short answer: call ABC0 on your Activity instead of System.exit(). This will hide your application until the user wants to use it again.

The longer answer starts with another question: why do you want to kill your application?

The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.

As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.

So if you want to hide your application call moveTaskToBack() and let Android decide when to kill it.

You should really think about not exiting the application. This is not how Android apps usually work.

android.os.Process.killProcess(android.os.Process.myPid()); works fine, but it is recommended to let the Android platform worry about the memory management:-)

Use the finish method. It is the simpler and easier method.

this.finish();

Keep in mind that when working with applications that use persistent socket connections, the finish() method does not release the connection. Under normal circumstances, finish() is the best option, but if you absolutely need to exit an app and release all resource it's using then use killProcess. I've had no problems using it.

The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).

  1. Launch an activity using this Intent:

    Intent intent = new Intent(this, FinActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    
  2. In the target activity FinActivity.class, call finish() in onCreate.

Steps Explained:

  1. You create an intent that erases all other activities (FLAG_ACTIVITY_CLEAR_TOP) and delete the current activity.

  2. The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.

Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true) ensures that all objects will be finalized and garbage collected when the the application exits. For example:

public void onDestroy() {
super.onDestroy();


/*
* Notify the system to finalize and collect all objects of the
* application on exit so that the process running the application can
* be killed by the system without causing issues. NOTE: If this is set
* to true then the process will not be killed until all of its threads
* have closed.
*/
System.runFinalizersOnExit(true);


/*
* Force the system to close the application down completely instead of
* retaining it in the background. The process that runs the application
* will be killed. The application will be completely created as a new
* application in a new process if the user starts the application
* again.
*/
System.exit(0);
}

Finally Android will not notify an application of the HOME key event, so you cannot close the application when the HOME key is pressed. Android reserves the HOME key event to itself so that a developer cannot prevent users from leaving their application.

Start the second activity with startActivityForResult and in the second activity return a value, that once in the onActivityResult method of the first activity closes the main application. I think this is the correct way Android does it.

I use this method to close the Activities!

public static void closeAllBelowActivities(Activity current) {
boolean flag = true;
Activity below = current.getParent();
if (below == null)
return;
System.out.println("Below Parent: " + below.getClass());
while (flag) {
Activity temp = below;
try {
below = temp.getParent();
temp.finish();
} catch (Exception e) {
flag = false;
}
}
}

Try the following. It works for me.

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
am.restartPackage(componentInfo.getPackageName());

It's actually quiet easy.

The way I do this is by saving a flag in a static variable available to all. Then, when I exit, I set this flag and all my activities check this flag onResume. If the flag is set then I issue the System.exit on that activity.

That way all activities will check for the flag and will close gracefully if the flag is set.

Run the second activity using start activity for result:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);


//This line is important
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);


startActivityForResult(intent, REQUEST_CODE);

Add this function to the first Activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(rquestCode == REQUEST_CODE)
if(resultCode == RESULT_CANCELED)
finish();
}

And add this to the second Activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {


Log.i(TAG, "Back key pressed");
setResult(RESULT_CANCELED);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

You are wrong. There is one way to kill an application. In a class with super class Application, we use some field, for example, killApp. When we start the splash screen (first activity) in onResume(), we set a parameter for false for field killApp. In every activity which we have when onResume() is called in the end, we call something like that:

if(AppClass.killApp())
finish();

Every activity which is getting to the screen have to call onResume(). When it is called, we have to check if our field killApp is true. If it is true, current activities call finish(). To invoke the full action, we use the next construction. For example, in the action for a button:

AppClass.setkillApplication(true);
finish();
return;

I use this:

1) The parent activity call the secondary activity with the method "startActivityForResult"

2) In the secondary activity when is closing:

int exitCode = 1; // Select the number you want
setResult(exitCode);
finish();

3) And in the parent activity override the method "onActivityResult":

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int exitCode = 1;
if(resultCode == exitCode) {
super.setResult(exitCode); // use this if you have more than 2 activities
finish();
}
}

This works fine for me.

It is not recommended, but you can still use this. Better go with this solution in case you need to quit the app.

According to me, the best solution is to finish every activity in your app like below.

Step 1. Maintain a static variable in mainactivity. Say,

public static boolean isQuit = false;

Step 2. On click event of an button, set this variable to true.

mainactivity.isQuit = true;
finish();

Step 3. And in every activity of your application, have the onrestart method as below.

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
if(mainactivity.isQuit)
finish();
}

This is what I did to close the application: In my application I have a base activity class, I added a static flag called "applicationShutDown". When I need to close the application I set it to true.

In the base activity onCreate and onResume after calling the super calls I test this flag. If the "applicationShutDown" is true I call finish on the current Activity.

This worked for me:

protected void onResume() {
super.onResume();
if(BaseActivity.shutDownApp)
{
finish();
return;


}}

Say you have activity stack like A>B>C>D>E. You are at activity D, and you want to close your app. This is what you wil do -

In Activity from where you want to close (Activity D)-

Intent intent = new Intent(D.this,A.class);
intent.putExtra("exit", "exit");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

In your RootActivity (ie your base activity, here Activity A) -

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("exit")) {
setIntent(intent);
}
}


@Override
protected void onResume() {
super.onResume();
if (getIntent() != null) {
if (("exit").equalsIgnoreCase(getIntent().getStringExtra(("exit")))) {
onBackPressed();
}
}
}

onNewIntent is used because if activity is alive, it will get the first intent that started it. Not the new one. For more detail - Documentation

I solved a similar problem: MainActivity starts BrowserActivity, and I need to close the app, when user press Back in BrowserActivity - not to return in MainActivity. So, in MainActivity:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "sm500_Rmt.MainActivity";
private boolean m_IsBrowserStarted = false;

and then, in OnResume:

    @Override
protected void onResume() {
super.onResume();
if(m_IsBrowserStarted) {
Log.w(TAG, "onResume, but it's return from browser, just exit!");
finish();
return;
}
Log.w(TAG, "onResume");

... then continue OnResume. And, when start BrowserActivity:

    Intent intent = new Intent(this, BrowserActivity.class);
intent.putExtra(getString(R.string.IPAddr), ip);
startActivity(intent);
m_IsBrowserStarted = true;

And it looks like it works good! :-)