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.
Related
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.
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
I have 3 fragments : A, B and C.
A contents a list of element, when you chose an element from the list, it loads B fragment. Inside B, i have a button showPreview, a click on that button loads the C fragment.
I already implemented all of this, and it's working pretty nice.
Now what i want is to add, in A fragment, a shortcut button on each item, to access the preview (C fragment) without showing B fragment on UI like in google play application, you can download an app by clicking on Three dots -> Install, without opening app detail page.
Presently, when user clicks on shortcut button, i load the B fragment first (shown in UI), after i call previewButton.performClick() to click programmatically on the showPreview button. But that's not what i want because i am obliged to show B first, let it load entirely before making a performClick().
I have read about FragmentTransaction methods (attach/detach, add/remove etc.), about fragment lifecycle, etc ... without solution.
So my questions are :
how can i load B fragment without showing it on the UI ?
If that's impossible, how to do the same thing as google play application ?
Finally I resolved the issue. My problem was that I haven't seperate Views from Model. I didn't have to use performClick() anyway, I didn't have to depend on Button click.
I resolved it by just creating a method which does nicely what I want, after I use this method on button click and after shortcut list choice.
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.
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.