Back key infinitely loops back to same activity - android

I am programming an Android application and have a curious issue.
My application has a LoginActivity that defines the filter for launch events.
As soon as login is complete, it starts the "Home" activity using startActivity(new Intent(LoginActivity.this, HomeActivity.class)) and stops the LoginActivity using finish().
The HomeActivity is a simple dashboard with notifications, overriding onCreate and onStart. Also it updates the some content icons using an AsyncThread.
The problem is this: If I hit the Home-Button to exit my app and then use the "recent" menu (holdpress the Android-Home Button) to reopen it, the back-key is 'broken' in my app: Pressing it will not finish the HomeActivity, but instead loop back to the same activity:
Meaning ... HomeActivity <- HomeActivity <- HomeActivity <- HomeActivity ...
I have not used any hacks to override the backstack or back key behaviour.
Anyone got a clue what the cause of this may be?
TIA, Patrick

Maybe your login activity detects that login is complete and sends you immediately back to your home activity. That should be visible from the log (ActivityManager, START intent ...)
In that case it may be a good idea to play with the backstack

Related

How to configure the back stack when activity is launched by BroadcastReceiver?

I have an app with MainActivity.java which has a button that when clicked launches Main2Activity.java. Main2Activity.java is added to the project in Android Studio using New/Activity/Basic Activity. It has a back arrow at the top and touching it will return the app to MainActivity. AndroidManifest.xml has the following for Main2Activity:
android:parentActivityName=".MainActivity"
The app also has a BroadcastReceiver which, after performing its work, launches Main2Activity.java. When started this way (when the app is in the background), the back arrow in Main2Activity does not go back to MainActivity but instead exits the app.
I would like the back function of Main2Activity to always go to MainActivity. According to Navigate up with a new back stack, there seems a way to do this, but the example is meant for an activity launched by another app.
How do I set the back destination for an activity launced by a BroadcastReceiver?
There are two ways of doing this:
Providing the Up Navigation as mentioned in the link quoted by you in the question. It should work even when you're navigating to the Activity through a broadcast.
Use startActivities from the BroadcastReceiver to start both MainActivity and Main2Activity. This allows you to create a backstack when starting an Activity and it's parent is not on the stack.
I prefer approach 2 simply because it works even when you press the hardware back, not just the back button on the Action Bar. For achieving that with approach 1, you'd also have to override onBackPressed.

Android crash on one activity delegates to previous activity

I have two activities, first one is LoginActivity, the second one is MainActivity. When an error occurs on MainActivity I get force close dialog which is ok, but why does my app goes to previous activity and runs some code on it and then I get another force close?
Shouldn't app close immediately when error occurs on particular activity, instead of after crashing on MainActivity I see in log that it executes some code on LoginActivity as well and if not not properly handled, it crashes there too so whenever I get an error on MainActivity it triggers one more force close dialog on LoginActivity?
What do you expect the user experience to be? What if they press the back key?
When an Activity crashes the app opens the last Activity that was running as it's at the top of the activity stack. So what you're experiencing is expected behaviour. But you weren't expecting it so it seems you may have structured the flow of your app incorrectly.
When the user starts your app they should be presented with the main activity, when they try to log in they should be presented with the login activity, once login is complete the login activity should be finished so it does not remain in the activity stack.
If you do not finish the login activity and instead start the main activity then the user will be able to press the back key to return to the login activity which is not ideal, as they are already logged in.

How to clear activity stack trace on back press event?

I have created an application that has multiple pages and navigation from one to another represents a crucial flow. I don't want the user to be able to press the back button and escape the activity without first warning him and then finally deleting all stack trace such that when the activity is launched again it starts afresh.
As of yet I have been using something similar to the function below :
#Override
public void onBackPressed()
{
this.finish();
Intent int1= new Intent(this, Home.class);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(int1);
super.onBackPressed();
}
But sometimes when after quitting the application when I launch it again it restarts from some random page or the one from where I quit the application (basically not the home screen from where it is expected to start)
I cannot think of a cleaner way to quit the application other than clearing all the previous activity flags as described in the code.
Any help on the above is appreciated!
EDIT :
Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces). Such that in case someone re-lanches the application it will re start normally from the main page.
You don't need any of this custom code in onBackPressed(). All you need to do is add this to all of your <activity> definitions in the manifest (except the root activity):
android:noHistory="true"
This ensures that none of your activities (expect the root activity) is recorded in the back stack. When the user clicks the BACK key, it will just return to the root activity.
Another benefit of this is that if the user leaves your app (by clicking HOME or by pulling down the notification bar and clicking on a notification, when he returns to your app it will also just return to your root activity.
Anytime during the flow of my activity if the user presses the back
button, I want the control to be thrown back to the main page
(clearing all the previous activity stack traces).
This can be done just by finishing all the activities as they move forward, except the MainActivity.
Such that in case someone re-lanches the application it will re start
normally from the main page.
Is it like if user is in Activity_5 and uses Home Button and relaunches the app again, MainActicity must appear?
IF so, you can call finish() in onPause() of every Activity except MainActivity
EDIT:
Might not be the perfect solution, but this is what I did to achieve exactly the same(logout in my application):
OnBackPressed() in any activity updates a boolean shared preference say backPressed to true and in onResume() of all the Activities, except MainActivity check its value and finish if true.
#Override
protected void onResume() {
super.onResume();
SharedPreferences mSP = getSharedPreferences(
"your_preferences", 0);
if (mSP .getBoolean("backPressed", false)) {
finish();
}
}
Back Button is used to go back to the previous activity. So i would not override the back button to clear activity stack. I suggest you use a Action Bar for this purpose. Navigate to Home Screen of the application using the application icon.
http://developer.android.com/guide/topics/ui/actionbar.html
Also check this link and comments below the answer by warrenfaith
android - onBackPressed() not working for me
#Override
public void onBackPressed()
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
you can use that code, it's work for me!

Why my android application is exiting when I press the Back key

I am developing a small application at the moment it consists on 3 Activities.
Now when I start my application it starts fine and I can navigate from activity1 to activity 3 properly and without any problem.
Activity1-->Activity2--->Activity3
The problem comes that when I press the back button of my mobile device to go back to activity2, the application simply closes.
Can somebody please suggest how to figure it out what is happening.
that is how i am going to Activity2 from Activity2
Intent activity3 = new Intent(Activity2.this,Activity3.class);
Activity2.this.startActivity(activity3);
Activity2.this.finish();
Note:I am not using emulator I am using mobile and doing all the debugging directly on the mobile.
Thanks
This is because you are calling the finish() function which removes the activity from the stack. Remove the line Activity2.this.finish(); and you should be going back the way you wanted.
Because you call finish() method.
Due to this from activity stack your last activity is removed so that's way your current activity is finished on backPressed().
To overcome this You must remove
Activity2.this.finish();
from your code.

Android reuse activity from activity stack and clear it

I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3

Categories

Resources