Android FragmentActivity not returning to previous - android

I have 3 fragment activities: home screen, a news feed, and an article view. Clicking the button on the home screen opens the news feed, clicking an article in the news feed opens the article view. But when I click the "Back" button in the article view, it returns to the home screen.
Is there any reason it would be skipping the previous activity? How do I make sure it goes back to the calling activity rather than the home screen?

I fixed the problem by adding android:alwaysRetainTaskState="true" to the activities that opened the article.
I think the issue was that the activity was getting destroyed when I switched to the article activity and when it tried to go back there was nothing there.

Related

Fragments recreated with Jetpack's Android Navigation components

I have two fragments (for ex. fragmentA and fragmentB).
first , in fragmentA use findNavController().navigate(R.id.action_fragmentA_to_fragmentB) to navigate to fragmentB .
then , in fragmentB if you want to back to fragmentA. there are two ways in below :
just press back button : fragmentA's onCreate() won't be called
findNavController().navigate(R.id.action_fragmentB_to_fragmentA) : fragmentA's onCreate() will be called
why?
The reason that the back button doesn't call onCreate of the fragment is by design. Users would not expect the back button to call the onCreate, or create, your fragment again.
As an example, think about when you open the YouTube app on Android and you are shown your home screen, populated with videos based on your interests. When you tap on a video after a bit of scrolling and then midway through the video decide to go back by pressing the back button, you expect the app to go back where you tapped on the video, with the same amount of scrolling you had done, instead of reloading your entire home page again filling with new videos and resetting you to the top of the screen.
Similarly, back button in your app should do the same. If however, you want your back button to behave differently, android does provide a way to do this. Refer to this for that.

Image is picked from gallery but not viewed

I am developing a quiz app in which I have a profile page were the user can change their image when they needed. It was working properly, the profile page of the user is a fragment not an activity. I faced a problem recently that am able to pick image from gallery but cannot view it the fragment image view instead of viewing that fragment my last (i.e) the activity before this fragment is called. Can anyone please help me reg this.. ?
Let me explain my prob clearly:
I have a navigation drawer activity which contains four fragments. first I have a splash screen activity next comes the navigation were one of the fragment appears, which contains a couple of onClickListeners() in it which navigates to the corresponding activity. What I did here is when I press back button from the fragment I got the recently viewed activity since I killed that activity life cycle by giving finish().
Here raised the next problem, now when I try to press back button I got the fragment itself viewed and then the app got closed. For this I found a solution of giving finish() in onstop() in my navigation activity which holds all the fragments. So now that problem got solved for me.
Again I got another issue (i.e) in that four fragments one is profile page, where the user can update their profile photo from gallery. The problem here is when I try to change the profile photo it goes inside the gallery and when I picked a image, the fragment life cycle onStop() method is called and the app gets closed by itself. I want to stop this from happening.. please have a look and suggest me with some ideas.

What is the proper UX for pressing back with navigation drawer fragments?

Suppose I have 5 drawer items in Activity B. If I select 2, then 3, then 1, What's the proper UX when I press back 1 time? Go back to fragment 3, or go back to Activity A? I see SO posts where people add to the fragment backstack, so it looks like the proper UX is going back to fragment 3. But what if I kept clicking different nav drawer items? It would take a long time for me to return to Activity A.
Try building out the solutions and testing the solutions on a friend. I would also try a solution where a user has a "home button" to return to Activity A. The back button could be set to returning to the last fragment.
If the user is losing any information while navigating back to Activity A and skipping the last fragments then I would consider that poor UX.
I see the overall trend is not adding the fragments to the backstack but going to the previous activity or exiting the app. The responses here have been helpful; thank you.

Android app error, the back button doesn't go back to the previous page.

I just noticed a pretty large amounts of error in my code.
My app has quite a few pages (in the form of fragments) and a splash screen.
The manifest is set up to load the splash screen and then start the mainactivity class.
When the mainactivity class loads up (after the splash screen) it shows the home page of the app, thats fine, but when i load up another fragment I always just assumed that if I hit the back button on the device it would take me back to the page opened before hand but instead of that it takes me back to the splash screen and traps me there.
Any help guys?
After you launch the main activity from the splashscreen you should call finish() on the splashscreen to ensure that it will not show up again (after all, it will not be needed anymore, so why keep it alive?). Then you should override onBackPressed on your main activity and ensure that instead of calling super, and finishing the current activity, it will call your fragments adapter and change the current visible fragment.
You need to use addToBackStack functionaliy of fragmentTransaction
Take a look at docs and example

Back Stack, Splash and TabActivity

I've searched SO and found several answers to the question in general, and have tried them all and am not having success. I really don't have my head around how the back stack works, Intent flags or the finish method. Here's my setup:
On application start-up, there's a splash screen where a couple AsyncTasks run in the background and check a couple webservers for updated content. ProgressDialogs report status. When complete (via the last onPostExecute), I launch a new Activity ("Home"). This seems to reflect some of the other posts, but I think my kludge is due to Home being a TabActivity, with 4 tabs, that initially calls setCurrentTab on tab 0.
So, using the suggestions previously posted:
android:noHistory="true" on the Splash activity
calling Splash.this.finish() after it launches the Home TabActivity
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_CLEAR_TOP
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_NO_HISTORY
The users sees the splash, the TabActivity launches, the user clicks to another tab, then hits back - the application closes (not force close - just closes back to the devices home screen).
If I don't use any of those, when the user hits back after changing to another tab, they go back to the Splash screen and are stuck (I could add a button or something to take them to the Home TabActivity but that's not optimal).
The desired result is that the user sees the Splash, gets taken to the Home TabActivity, clicks another tab, then hits back, he should be taken back to the initially set tab (tab 0).
Any insight is appreciated.
TYIA
The back stack is actually officially called the activity stack - every time you start an activity, that gets pushed onto the top of the stack (unless you set one of those flags you mentioned).
This means that unless each tab in your main app is a separate activity, then the default back key behaviour will be to leave your main app activity.
You can control this by taking over the back key or by overriding the tab switching behaviour to start different activities.

Categories

Resources