Issue on Handling Back Button - android

My App starts with a splash screen loading background data from remote server. After completion of data load, An Activity(say B) launched.
This activity B (A photo Gallery of different Animals), when pressed back launches SplashScreen, so inorder to solve this, i prompt user if it really wants to exit the app, if clicked yes, exit closes.
private void exitApp(){
B.this.finish();
}
My issues comes here.
Since Activity B, when clicks on Particular Animals say DOG,
Intent intent = new Intent(B.this,C.class)
startActivity(Intent);
takes the user to Activity (C) reviews of particular Animals.
When back button is pressed on C, takes me to B, thats fine.
Since B has menu options, such as bookmark,Home.
i do Launch BOOKMARK as with
sartActivity(B.this, Bookmark.class);
Since BookMark has menu for HOME i.e Activity B.
And now when i pressed back, it prompts me to Exit but it does exit the app rather takes me to BOOKMARK.
Can i solve this issues? As i was reading the doc, i found
`finishActivityFromChild(Activity, requestCode);`
can this help me achieve?
Let say when a user is prompted to exit the application. I simply want to remove all the stack of activities.
Can it be done?

add below tag in your AndroidManifest.xml file under BookMark activity
android:excludeFromRecents="true"
and include below tag in your HOME i.e Activity B.
android:launchMode="singleTask"
and let me know if it worked for you.

Related

Android back button after clear task and set new task

So I have three activities A, B, and C (A>B>C). Before I startactivity(C) in B, I clear task and set new task to make C become a new root. Everything works as expected. When user clicks the back button in C, they go back to the main screen, but when the user clicks the app again, it redirects them to A. My thought is that there is nothing in the stack, so it just shows the default activity? How can fix this? so that when user clicks on the app again, then it will show C activity?
Which Activity is going to launch through clicking is defined in AndroidManifest.xml, see this official tutorial
If all you need is to stop user from going back to Activiy A and B, maybe you can just override the onBackPressed() and don't clear tasks. In this way you can still have user navigated to C when the icon is clicked.

how to clear background data when sign out

I have been writing a multiple accounts manage module. What I want to do is to lead user to Accounts list activity when signing out. The problems is when user press back button, it resumed the background activity and user still can use the app even signed out. And it doesn`t work either when I cleared account info in database and SharedPreference.
For better understanding I describe the problem again.
For example, I have three activities, A, B and C. A works as main activity with a list, B works as the settings menu activity and C works as the account list Activity.
When I navigate from A to B click "Sign Out" menu in B, then the process flow goes from B to C.
Because it asks user to choose (if has) or login an account.
Now the problem is when user press back button, it can go back from C to A rather than exit the app (go to home screen). See the screenshot blow.
Since user already signed out, I doesn`t make sense to navigate back from C to A. C should be the only visible Activity at this circumstance.
But I don`t know how to implement this, I already clear account info in local storage, so it should not be the settings problem.
So how to clear the background activity A?
Any comments will be greatly appreciated.
Maintain a flag suppose "isLoggedIn" in shared Preferences or database. In your launcher/first activity check this flag and decide whether to call login activity or direct xyz activity. And when you clear data again call your launcher/first activity with clear_top flag in intent.
try this code in Activity C
#Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
}

Android app navigates to launch activity when coming from background

I have 3 activities ( say A, B, C were A-is the Launch activity). When I press home button when at activity C, app goes into background. After that, I took my app through all apps list menu. At that time, my launch activity is showing (activity A). When I press back button, It goes to previous activity (C). I want to retain in the same activity (C) while coming back from background. When I run application through Eclipse, It works fine. But when I send Apk file through mail and run it in device, It fails (previous problem occurs ).
I tried with
android:launchMode="standard"
AND
android:alwaysRetainTaskState="true"
in my launch activity (Login activity or A). Any body please help me, Thanks in advance.
Follow following steps to insure that you are following the right practice:
1. Make sure you are using finish(); on the Activity A or B if you want to finish it and dont if you want the back button functionality.
2. Try Implementing onpause() and onresume() even if you are not going to perform any functionality in them. Use super() for them there.
3. Also, in android when you start an activity by clicking on the icon instead of resuming it from already running activities, it exhibits a different behaviour.

Android Activity back stack and multitasking support

I have an app that supports multitasking (working in the background), however I have run into problems with the android backstack.
This is what I have:
Activity A starts Activity B for result, so...
Activity A --> Activity B
If when at Activity B the user long presses the home button and switches to another application (say the browser for example) and then long presses the home button again and comes back to my app, they will be at Activity B, however the back stack at this time will look like this:
Activity A --> Internet Browser --> Activity B
So when I do finish() to send back a result from my Activity B it does not come back to my Activity A, but rather to the Internet Browser...
This is also the case if the user doesn't use long press of the home button, but also uses the home button to come back to their launcher and then uses long press home button to come back to my app. In this case the back stack is even worse:
Home Launcher --> Activity B
So when I do finish() on Activity B, the user gets back to their home screen and they can never get back to Activity A except for if they go and start the app again from their app drawer.
Is there any way to implement multitasking work in this case? Activity B needs to always return back a result to Activity A no matter what the user opened in-between these two.
OK. After long hours of research and trying various things, here's the solution to the problem. Hopefully this helps others...
The solution is pretty straight forward and simple, in AndroidManifest.xml
set android:launchMode="singleTask" for Activity A
set android:noHistory="true" for Activity B
This way Activity B will be removed from the Stack if we go to another app like the browser or exit to the home screen, so when we come back to our app we get back to Activity A.

switching between activities in android

Hi I'm writing an app that has multiple activities. Right now it starts at the home screen, then when the user presses a button it starts a new activity and goes to another screen, then the user enters in information and presses a button to start another activity and another screen.
I have a menu setup so that from whatever the activity the user is in they can get back to the home screen. What I want it to do is kill all the current activities and just take the user right back to the home screen, so there is only one activity running again. How can I do this?
After sending the Intent just call finish() and the activity you're leaving will be closed.
Just don't do that on your "homescreen" activity. That way whenever the user starts an activity through one of your activities when he presses the back hardware button he is going to get back to your "homescreen" activity.
I figured it out. If you add myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); then when you run startActivity(myIntent); it clears all activities except the one myIntent is starting.

Categories

Resources