Redirecting to home in android - android

Redirect to home. I opened different App from my App using intent. When I click back it was redirecting to my application. But at that time I want to close that App or redirect me to home. I tried with finish() and destroy(). But those are not working ?
Intent nextIntent = new Intent(Intent.ACTION_MAIN);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.setComponent(new ComponentName(packageName,package_running_cls));
NotFirst.this.finish();
startActivity(nextIntent);

If finish() is not working you should override onBackpressed() method and in onbackpressed you can use intent to go to your HomeActivity like this
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent in=new Intent(this,YourHomeActivity.class);
startActivity(in);
this.finish();
}
try this, it may help you.

You can use moveTaskToBack(true).
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.

Related

Finish app from any activity [duplicate]

This question already has answers here:
Finish all activities at a time
(21 answers)
Closed 3 years ago.
I want to kill my app from a exit button on my navigation drawer. the drawer has a exit button that is suppose to just kill the whole app (all activity) and gets the user out of the app .
When i am using finish() in the first activity its working but when i go into the app and call finish() the activity gets killed and returns to previous activity but never really kills the app.
Tried to use System.exit(0) but no luck.
Extra Information Might Help
I have all my activity started with android:launchMode="singleTop" . it means all those activities that are already created will not be created again and just reordered to front on calling.
Do anyone have any suggestion for this , please do help.
Update
I want to make some updates here as my question looks like this SO Question.
As i have already said i am using a android:launchMode="singleTop" . and this answer is not working for my case. I have to call onCreate() to make this happen but it is not my case.
Use below code
public void AppExit()
{
this.finish();
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
Call above function to exit from App.
First, you need to decide if you just need to take the user out of the app, or if you also need to finish() all of your activities. It depends on your needs, but my guess is that in most cases most users won't know the difference, and Android is designed to have many apps having activities still running. If this is all you need, then you can just use an Intent to take the user to the home screen.
Intent intent = new Intent(Intent.
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
If you actually want to finish() your activities, then you need a mechanism to inform all currently running activities that the user is exiting the app. You can use something like EventBus to send an event that will be received by all registered subscribers; if you don't want to use a third-party library, you can do the same type of thing with LocalBroadcastManager. To do that, you would register a BroadcastReceiver with an IntentFilter for a custom action and broadcast an Intent with that action when the user wants to exit.
In either case, every activity should receive the signal and call finish() on itself. Note that the subscription (in the EventBus case) or the registration of a receiver (in the LocalBroadcastManager case) must be still active after onStop(), so I would register in onCreate() and unregister in onDestroy().
Use below code in your exit button
Intent intent = new Intent(this, ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And, in your ActivityOne.class oncreate() method just put below code
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
As a few answers say, call finish(); after you start an intent from an activity. That will make sure the current activity is the only running activity. Also try super.finish(), which will call finish() at parent activity.
You can alternatively use only one activity with many fragments. That way, if you call finish() in the exit button OnClickListener code, you'll exit the app. This will also save a lot of coding since you will be defining the navigation drawer once.
First of all, Android apps are not intended to be killed other that by the system, which attempts to keep them in memory in case the user will return.
But, if you have to do it, try this:
android.os.Process.killProcess(android.os.Process.myPid());

How to exit from application permanently in Android

I am doing one application here I have one exit button when I click button,that time I need to exit from application..I tried using below code app is closing but it still running in taslmanager..but when I click exit button I should close app as well as I should remove from task manager how it's possible.
public class MainMenu extends Activity {
Button exit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
exit=(Button)findViewById(R.id.btn1);
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
try this to exit the application
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
Please think really hard about if you do need to kill the application: why not let the OS figure out where and when to free the resources?
Otherwise, if you're absolutely really sure, use
finish();
As a reaction to #dave appleton's comment: First thing read the big question/answer combo #gabriel posted: Quitting an application - is that frowned upon?
Now assuming we have that, the question here still has an answer,
being that the code you need if you are doing anything with quitting
is finish(). Obviously you can have more than one activity etc etc,
but that's not the point. Lets run by some of the use-cases
You want to let the user quit everything because of memory usage and
"not running in the background? Doubtfull. Let the user stop certain
activities in the background, but let the OS kill any unneeded
recourses. You want a user to not go to the previous activity of your
app? Well, either configure it so it doesn't, or you need an extra
option. If most of the time the back=previous-activity works,
wouldn't the user just press home if he/she wants to do something
else? If you need some sort of reset, you can find out if/how/etc
your application was quit, and if your activity gets focus again you
can take action on that, showing a fresh screen instead of restarting
where you were. So in the end, ofcourse, finish() doesn't kill
everthing, but it is still the tool you need I think. If there is a
usecase for "kill all activities", I haven't found it yet
Use this flag:
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.
This flag will notify the OS to remove this app/activity from the cache of recent apps.
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
You can also add
android:noHistory="true"
android:excludeFromRecents="true"
in your AndroidManifest file for the activity where you don't want to see in recent apps.
Android doesn't allow you to directly exit from the app.you just have to remove the your current activty from the stack before going to any new activity.
class first extends activity{
oncreate(){
Intent i = new Intent(getappcontext(),nextactivity);
stratactivity(i);
first.this.finish();
}
}

Onclick back button the application stops

I have one problem regarding my app,when i click back button my application is getting finish.i need to prevent it.How could i make it .
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
startActivity(setIntent);
}
I have tried something like this.But its not working .Actually what i need is when i will click back button ,the application should not finish it should run in background.can anybody help me out.#Thanks
Try doing it like this:
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
By doing this, your activity will not be destroyed (i.e. onDestroy will not be raised). But also, there's no guarantee that Android will preserve your activity for long.
In case, you are running a process that you want to keep on running even in background, then I would suggest you to go for Service or IntentService.
What do you mean by "it should run in background"? Why would you want that? If the user wants to keep the app opened, he can use the home button and the app won't be closed, if he presses the back button he wants to close the app. If want to have something that is still running even after the user closes the application you should take a look at Service http://developer.android.com/reference/android/app/Service.html, this will continue running even after the user closes the app

Exit an android app

How to close an android app if more than one activity is in active state?
A blog post entitled Exiting Android Application will show how to exit an Android app:
When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
I got an easy solution for this problem
From the activity you press the exit button go to the first activity using the following source code. Please read the documentation for FLAG_ACTIVITY_CLEAR_TOP also.
Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Now overide onResume() of the first activity using finish()
The answer is simple: You really do not need to 'close' an Android application. If no activity is shown any more, the system will kill the process after some time. The users can close activities by pressing the 'back' button. Reto Meier explains it pretty well here:
http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
You might also want to read this thread; it is very helpful to say the least: Quitting an Android application - Is it frowned upon?
Well, you shouldn't close your applications, as the system manages that. Refer to the posts/topics in the other answers for more information.
However, if you really, really want to, you can still call System.exit (0); like in any other Java application.
EDIT
ActivityManager actmgr = (ActivityManager) this.getSystemService (Context.ACTIVITY_SERVICE);
actmgr.restartPackage ("com.android.your.package.name");
I remembered something. I was trying to use this code to restart my application, but it only managed to kill my app. You can try it and see if it works for you.
I asked a similar question a couple of weeks back. Do go through the answers and comments for more perspective and possible solutions.
IMO quitting an application depends on what your application does and the user expectations. While I understand the rationale on not having a quit button I also do believe that it's a choice that the application designer has to make based on the situation.
Once your last Activity looses focus, Android will unload your process according to the current system needs / free resources.
You shouldn't really care about that - just use the lifecycle onStart, OnStop etc... to manage your state.
If you want to exit from one Android activity, this will bring you back to the previous activity or another activity from a specific place in current activity.
finish();
System.exit(0);

How i can close/exit button on my app?

i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);

Categories

Resources