how to launch a fragment of an activity from another activity? - android

I have one activity 'A' which has many buttons on it to call several corresponding fragments of another activity 'B'.
Activity 'B' contains a navigation drawer where i have created fragments for all the items that we have in the navigation drawer.
So, how do i immediately launch suppose 'fragment 1' ( i.e one of the items of the navigation drawer), inside Activity B, when Activity A calls Activity B.

Add an extra to your Intent which defines which Fragment should be loaded.
intent.putExtra("fragment","FragA");
In your onCreate in Activity B:
String fragment = getIntent.getStringExtra("fragment");
// Do something to load correct fragment

Related

Update viewpager fragment specific position and get back to previous while backpress

I have a view-pager with two fragment in it . based on my logic i want to replace the fragment on a specific position with another fragment but when user back-press i want to go back to previous fragment i replaced with it
for clearance
i have 4 fragment A, B, C, D initially i added A and B to view-pager, then i replaced B with c in view-pager , So that i want is to go back to fragment B (replaced by C) from C (current) while pressing back button instead of going back to another activity
you can do using assign constant variable to your fragment or assign tag at time of Fragment Call.
I manage using VariableName CurrentFragment which update value on Fragment Lunch. Then after onBackpress method of your activity i handle the fragment based on Constant Variable.
Visit below Stackoverflow link where i put solution step-wise to handle multiple fragment on backpress.
Click Here to View StackOverFlowAnswer

How to move activity to another activity with specific fragment target?

Is there a way that from the activity i want to go to another activity with multiple fragment and select a fragment that i want to focus it.
Assuming you want to move from Actvity A to Activity B and make Activity B show a particular fragment. You need to pass an extra containing some form of identity(Eg. the name you gave to your fragment) of the fragment you want to show to Activity B. Then in onCreate in Activity B read the extra you passed to it to determine which fragment to setup in Activity B

How can I navigate from an activity to another using Navigation Component and removing the first activity from the stack?

In my app I have two activities (AuthenticationActivity and MainActivity), each one with a nav graph and a big flow of fragments. I've created an action to navigate from a fragment of the AuthenticationActivity's graph to the MainActivity but it does not remove the AuthenticationActivity from the stack even if I set 'popTo' to the Authentication graph id.
finish authentication activity after navigate method:
button2.setOnClickListener {
view.findNavController().navigate(R.id.action_addItemFragment_to_mainActivity)
(activity as AuthenticationActivity).finish()
}

Proper navigation between activities using navigation drawer and back button?

I have the following situation:
One Main ListActivity. The list consists of POJO objects, each item is a different POJO.
Each POJO contains different groups. For each group I made different Activity.
Lets say POJO1 contains groups A, B and C, when pressed, ActivityA will be opened and the navigation between the rest activities ActivityB and ActivityC is done using NavigationDrawer.
All group activities implement a custom class MyNavigationDrawerClass which is the core of the Navigation Drawer in my app.
Each ActivityA, B, C, etc, has a SettingsActivity which is strictly individual and depends on from where it has been started.
Now the question is: How to navigate from any A, B, C, etc, activity to the Main ListActivity every time the BackButton is pressed?
I will provide an example:
App is started - Main List Activity is showed;
Item is pressed and ActivityA with NavigationDrawer is started;
The NavigationDrawer is opened and ActivityC is opened;
When BackButton is pressed, the user is navigated back to ActivityA, but my desire is to navigate the user to the Main List Activity
What I tried:
To put android:noHistory="true" for every ActivityA, ActivityB and ActivityC in the AndroidManifest. That did not work because when I open the SettingsActivity for example from ActivityA and return, the user in navigated to the Main List Activity, and not to the ActivityA - it's confusing and misleading.
To override onBackPressed in MyNavigationDrawerClass and start the Main List Activity using Intent. That did not work either because there are still previous activities in the stack and when the BackButton is pressed from Main List Activity, the user is navigated to them.
I hope I explained the situation clearly.
Any help will be appreciated.
You can override the public void onBackPressed(){...} for redirect the user where you want like this :
public void onBackPressed()
{
Intent intent = new Intent(this.getApplicationContext(), MainListActivity.class);
startActivity(intent);
}
look at this link can help you

Knowing when a fragment is left from / returned to

Android's Fragment's onResume/onPause methods are tightly coupled with the host Activity's lifecycle as shown here.
What I want to know is how to detect that a fragment is left from / returned to inside the app's navigation flow.
Example:
Say I have MainActivity and fragments A,B and C.
MainActivity adds fragment A, then B and then C.
How do I know fragment B was left (I now see fragment C).
Also, once I press on back, how do I know fragment B was resumed?
Edit:
Clarification: I want to know that from within fragment B (similar to the way an Activity works with onPause and onResume)
Try isDetached() method link here
Respectively there is isAdded()
Indirectly you question concern with Fragment LifeCycle.
And you can trace fragment navigation replace,remove,add using Fragment manager
PopBackStack : Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned.

Categories

Resources