Quitting application in Android - android

I want to quit application in Android. Just put a "quit" button, which kills my app.
I know I shouldn't do this. I know that this is not the philosophy of the OS.
If you know how it can be done, pls share.
In the app, I have many opened activities, so "finish()" will not do the job.
Thank you for your information in advance.
Danail

Way One
android.os.Process.killProcess(android.os.Process.myPid())
Way Two
System.exit(0);

Your answer helped me, Pentium10, but I needed to do one more thing :
I had to clear(close) all my previous activities with
Intent i = new Intent();
i.setClass(this, FirstActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
This means: every activity, started after FirstActivity, is closed. (In other words, this cleans the activity stack above FirstActivity). Then all I have to do is finish my FirstActivity.

Close all the previous activities as follows:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();
Then in MainActivity onCreate() method add this to finish the MainActivity
setContentView(R.layout.main_layout);
if( getIntent().getBooleanExtra("Exit me", false)){
finish();
return; // add this to prevent from doing unnecessary stuffs
}

Related

Intent - addBackStack(null) for Activity

We can use addBackStack(null) with Fragments and in this way the fragment won't open when we press back. But how we can do this with Activity? I could'n find an usage about that.
I also tried this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
but this is not working.
And finally addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) is working but when I take application to background and reopen it this activity is destroyed.
I understand there is no need for Flags. The problem was solved with adding finish() when open other activity.
Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();
startActivity(i);

Android kill all other activities on starting a new one

I have an android activity problem.
Here is how my process works:
Login Activity starts
Login successful. MainMenuActivity starts and LoginActivity is finished by me.
User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
I tried addFlags() and setFlags() both, didnt work
There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.
Can anyone help, please?
P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.
Use the combination of two flags like this:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This deletes all the other activities and starts this one.
Try this.
For api level <11
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.
I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will only work if Login activity is still active at the root (beginning) of the task stack.
To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.
Use FLAG_ACTIVITY_CLEAR_TOP like this-
Intent loginIntent = new Intent(this, Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();
Intent intent = new Intent(MainActivity.this, MyActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
in this example Mainactivity automatically closed and Myactivity2 Start.
and one thing you need to add finish();

Clearing all activities in android

I have 4 Activities 1.Home,2.B,3.C and 4.D. Whenever I start Home from Activity D I want to finish all other activities. I Used this code, but when I press back button from Home it brings me to the previous activity. What I did wrong here.?
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
You can try this,
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note: As described in FLAG_ACTIVITY_CLEAR_TOP documentation
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will work only for activities which are still in the activity stack. I believe you are finishing the Home Activity when going to B. So that CLEARTOP won't work.
Now try something like this.
You need to set an Extra with intent Of "D" to Home. Then you have to check the Intent extra in Home, call finish() if the extra matching
Intent intent = new Intent(contxt, Home.class);
intent.putExtra("urString",defaultvalue);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// Checking the intent extra at "HOME"
if(getIntent().hasExtra("urString")){
// manage your own way
finish();
}
In manifest.xml file set android:nohistroy="true" for all the activities

how I start activity which is caller and close activty which is callling at same time?

I want to call a activty but when I call actviy I want to finish my caller activty , How can I do this?
A:caller
B:calling
startActivity(new Intent(A.this, B.class));
finish();
I write this code but everything is closing.
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.
Try to do this:
startActivity(new Intent(A.this, B.class));
A.this.finish();

How to bring an activity to front in android?

I am using moveTasktoBack() function to send my activity to background.
I want to bring my activity to front when a timer in my activity finishes. I am using the phone back key to send the activity to back. How do I do that? Help please.
Exact Same issue that is mentioned on this Question.
Sloved it with following code snippet. i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); actauul which brings back the activity to front.
Intent i=new Intent(ApplicationStatus.this,NotifyActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.putExtra("ID_TimeLeft",String.valueOf(TimeLeft));
startActivity(i);
I think it should be FLAG_ACTIVITY_SINGLE_TOP.
You can use intent with the appropriate flags. FLAG_ACTIVITY_NEW_TASK to create a new task to contain your activity if one doesn't already exist. FLAG_ACTIVITY_REORDER_TO_FRONT to bring your activity to the front of the task if it's not already there.
Intent intent = new Intent(context, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Categories

Resources