Android: Communicating Between Fragments but with using back button as navigation - android

So I have the following situation and I'm not 100% sure how to solve it right it. It basically involves communication between the back button and two fragments.
Here is a Short Diagram
(Fragment1) (Fragment2) (Back button)
List of Recipes -> Recipe Filters -> List of Recipes
Obviously I'm doing addToBackStack etc. but I'm trying to rely on the back button for navigation instead of using the home button as I'm using that for a navigation drawer action right now.
Is there any way to pass information between fragment 1 / fragment 2 on a back button navigation action?
If not, what should be done instead. I'd like to avoid spawning an activity for the filters fragment instead.

You can do override onBackPressed() and put whatever logic you want there.Code sample:
#Override
public void onBackPressed()
{
// code to be executed when user presses back
super.onBackPressed(); // optional depending on your needs
}
Make sure you implement onBackPressed properly and they are no side effects.

Related

Using Android navigation controller and finishing the calling activity

I have an activity that is using the navigation component to flow through my fragments to get info from the user in a wizard style. I have a dead end fragment in my flow where I tell the user why they can't continue and I want to provide a done/cancel button that finishes the activity that the nav controller is in and leaves the user at the screen they left off on.
I tried adding the activity they launch the flow from as an action destination but that launches the activity as a new activity and isn't where the user left from.
I can grab the activity from the fragment and call finish() on that activity from the button click but that didn't seem like the way one is supposed to use the nav graph. Am I wrong? Is there a way to say in a nav graph direction to just exit the everything?
There's no navigate() operation that will pop activities off the stack - the only back stack that the NavController is aware of is the internal back stack it manages.
If you want to finish an activity, call finish() on your activity.

Detect pop in Navigation Component

In Navigation Component, how can one detect if fragment is brought to front from a pop event?
I go from A to B, now I close B using back key, it returns to A, now in A (in onViewCreated event) I want to detect it's coming from B.
If we're using a single NavController.
findNavController((R.id.nav_host_fragment)
.addOnDestinationChangedListener{ hostController, destination, _ ->
val push = currentBackStackSize < hostController.bacStack.size // else pop
// Then save current backstack size
}
This solution might not be always correct, but currently I can't think of an edge case. Please feel free to correct me.
Here is my solution.
In A, add a navigation argument with default value false (in the nav_graph.xml)
In B, add a navigation back to A. To handle back button pressed, add the following in onCreate()
requireActivity().onBackPressedDispatcher.addCallback {
val action = BDirections.actionBFragmentAFragment(true)
findNavController().navigate(action)
}
Now you can determine how A appears. Also, use popUpTo to handle circular logic properly. Let me know if you see any flaws in this approach.

Android phone built-in back button

I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}

Manually Access Android Back Stack

I'm having problems coming up with a solution to this problem.
Basically I have a load of tabs in my ActionBar. When each is touched the fragments from the previous tab are detached and the fragments for the new tab are added using replace (if they haven't been instantiated yet) or attached (if they have). I think I got this method from Google and it was working fine until now.
Example of adding a tab's fragments:
if(tab.getText().equals(context.getString(R.string.title_class_tab))) {
if(browser == null) {
browser = CourseBrowserFragment.newInstance(false);
fragmentTransaction.replace(leftContainerId, browser);
} else {
fragmentTransaction.attach(browser);
}
if(lessonViewer == null) {
lessonViewer = LessonViewerFragment.newInstance(false);
fragmentTransaction.replace(rightContainerId, lessonViewer);
} else {
fragmentTransaction.attach(lessonViewer);
}
}
and removing:
if(tab.getText().equals(context.getString(R.string.title_class_tab))) {
if(browser != null) {
fragmentTransaction.detach(browser);
}
if(lessonViewer != null) {
fragmentTransaction.detach(lessonViewer);
}
}
The problem arises from the layout I need for one of the tabs. Basically it's like the Gmail app. There are two fragments (let's say Panel A and Panel B) and when you push a button Panel A slides out, Panel B slides to Panel A's old position and a new, third one (Panel C) slides in from the right.
I had this working fine but now I've added the sliding-in FragmentTransaction to the back stack so that the user can touch the back button and Panel C will slide back out and Panel A will come back. Again, like Gmail.
Except when the user goes to a different tab this transaction is still on the back stack and executes if the user presses back. The fragments end up in crazy places. What I need to do is remove it from the back stack when the user navigates to a different tab. Is there any way I can do this? FragmentManager doesn't seem to let you manually remove things from the back stack and using the popBackStack() method doesn't just remove the transaction, it executes it. I want to remove it when the user navigates away and put it back when the user returns.
I think I can get a hold of the "Back Stack Entry" for this transaction using "getBackStackEntryAt" but it's not much good if I can't remove it and put it back in place when the user comes back to the tab.
The only possible solution I can think of is not using the back stack and overriding onBackButtonPressed instead. From there I could just do a reverse of the transaction if necessary.
Thanks for any help and sorry if I'm being incoherent.
Not sure if this would qualify as a solution but I ended up just not adding the transaction to the back stack and just doing a fresh transaction when the user swiped or pressed back. The transaction just did the reverse of the original one with animations etc.
The way I managed the back button is I set a boolean to true if I was in the layout showing Panel C. If the user swipes back into the Panel A layout or navigates away the boolean is set to false. I then overrode the onBackButtonPressed method in the Activity and if the boolean was true (ie: we're in the Panel C layout) I run that reverse transaction otherwise I just call super.onBackButtonPressed() (ie: perform standard back button behaviour).

When i Click Back Button in android i want to make changes in previous activity?

When I click back button in my app I want to change some data in previous activity.
public void onBackPressed() {
super.onBackPressed();
Log.v("Back Button ","Pressed");
}
I am trying to using this but cant help it out ?
do I need to maintain backstack of activities which back button internally does?pls help
i want to change menubar images (wriiten by me bottom of every activty in my app) item which is present in every activty but when I click menubar (clickable) it is not able to change menubar images as per activity changes.
You want to look at startActivityForResult. The previous parent Activity should spawn the next one with this call. Data can be passed backwards via extras (Bundle) in an Intent when returning to the parent Activity in onActivityResult - or you can simply use the result codes.
You can find more information here: http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Better use with onKeyDown. & onKeyDown Example
Just override this android's default method. It'll provide the changes with whatever you want.

Categories

Resources