How To Add A Back Button To The Fragment In The Action Bar Of An Opened Activity?
Solution 1:
Your SubSettingsActivity's theme should extend
Theme.AppCompat.Light.DarkActionBar
Then in java do in onCreate
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
Then in AndroidManifest where u declare SubSettingsActivity do:
android:parentActivity="YOUR_ACTIVITY"
YOUR_ACTIVITY is the activity that holds fragment
Edit: If you wanna navigate back to the SettingFragment its better you use Android Jetpack navigation component. It let's you seemlessly use fragments. Here is the link: https://developer.android.com/guide/navigation/navigation-migrate
How to implement Navigation Component :the simple way
Step 1: Add the required dependencies
implementation 'androidx.navigation:navigation-fragment:2.3.0-alpha05'
implementation 'androidx.navigation:navigation-ui:2.3.0-alpha05'
Step 2:
Goto res folder on Android Studio, right click it, new > android Resource File
Youll see the following dialog:
Note the Resource type, you must pick Navigation
Step 3: Since your fragments have been created, goto the activity xml that holds your fragment e.g activity_main.xml add the following:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/adView"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav"
android:id="@+id/nav_host_frag"/>
NavHostFragment is the class that will handle your fragments, navGraph will be your navigation file you created earlier.
Step 4: add your fragments to the navigation file, open your navigation file u created in step 2, switch the tab to design. As seen in the image below
Note yours will be empty. Use the add sign(1 in the screenshot) to add your fragments to the design view. Notice the (2 in the screenshot) start fragment, this the fragment that will be inflated first. Means when u open MainActivity, its the start fragment in the navigation graph that will be visible.
The curly arrows are the actions or navigations. For example, in my screenshot, start fragment goes to signup fragment and login fragment, it simply means where start fragment can navigate to within the fragments inside the graph. if its not defined in the navigation graph, it wont navigate. Other words, hey nav i will want to navigate from splash fragment to signup fragment or login fragment in the future, take note!
Step 5: when you want to navigate to a fragment from a fragment within the graph instantiate an instance of NavController:
private NavController controller;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
controller = Navigation.findNavController(view);
}
NavController will let you navigate and i usually instantiate it onViewCreated()
then:
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
controller.navigate(R.id.action_startFragment_to_anotherFragment);
}
});
To set your actionbar with the fragments, put the following code in onCreate() of host activity:
NavController mNavigationController = Navigation.findNavController(this,R.id.nav_host_frag);
NavigationUI.setupActionBarWithNavController(this, mNavigationController);
Also in host activity:
@Override
public boolean onSupportNavigateUp() {
return mNavigationController.navigateUp();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
Post a Comment for "How To Add A Back Button To The Fragment In The Action Bar Of An Opened Activity?"