Exit/Finish an app/activity - android

主宰稳场 提交于 2019-12-03 01:56:34

Use below code in your Act4'th Menu.xml's exit button -

Intent intent = new Intent(Act4.this, Act1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

And, in your first activity's onCreate() method just put the below code -

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

This will exit your app.

Krishna Suthar

Check out this link:

Click here

You can use :

@Override
public void onBackPressed()
{
     moveTaskToBack(true);
}

in all activities to close the app.

Prerak
Intent intent = new Intent(Act4.this, Act1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Clear the flag before you switch back to previous activity. It might help you.

Dipak Keshariya

finish previous activity when you are go to the next activity means write finish(); after startactivity(intent); and write below code for start first activity from fourth activity's button click event.

Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();

And Write Below Code on Your Exit Button's Click event in all activities.

finish();
Manikumar Gouni

Place this code in your app:

moveTaskToBack(true);
android.os.Process.killProcess(Process.myPid());
System.exit(1);

If you have another activity behind ( in the application activity stack), you could use finish() to exit the current activity.

However, the back function too suppose to remove the current activity from your application activity stack.

In my experience, it is very unreliable to rely on the activities in the back stack ( in the application activity stack). Because of that I used to exit each activity when I go deeper.

In your case:

Act 1 -> exit -> Act 2 -> exit -> Act 3 -> exit -> Act 4 -> exit -> Act 1 -> exit 

The above method will exit all the activities.

However, if you want to run some code in the background, it is better to rely on a "Service" rather than an "Activity". You can let the "Service" exit after doing its assigned work.

Do a 
<code>

try
{
finalize();
finish
}
catch(Exception error)
{
error.printStackTrace();
}
//This would exit your App neatly
</code>
R Earle Harris

The approach I use is to start all child activities with startActivityForResult. Then I can putExtra("STATE", some_value) on exiting any child and use the state value to determine what to do up through the parents.

If I want to exit the app from a deep child, "STATE" would be "exit" and each child in the hierarchy would simply get the StringExtra for "exit", do a putExtra("STATE", "exit") and call finish() and then the app would call finish() in the end. You can use this to achieve any desired state.

Using startActivityForResult to pass messages this way avoids having to pass awkward references down into the child activities and simplifies your approach to setting the desired state of the app.

Use android:noHistory = "true" in your AndroidManifest.xml file

    <activity
        android:name=".Act1"
        android:noHistory="true" />
    <activity
        android:name=".Act2"
        android:noHistory="true" />
    <activity
        android:name=".Act3"
        android:noHistory="true" />
    <activity
        android:name=".Act4"
        android:noHistory="true" />
Hakim Douib

I use this method :
1. Create static activity in the first activity that will close the remaining activity(s).
2. Then call it in the static method like this, it will close the app from wherever you call it.

public static Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity=this;
}
public static void CloseApp(){
    activity.finish();
} 
Muhammad Rehan

Tray this one

Intent intent =new Intent(MainActivity.this, ClassName.class);
  finish
startActivity(intent);
Vandana Surendranath

Start Activity1 using intent and try calling finishAffinity() in onBackPressed() of Activity1.

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