I have a HomeActivity
that implements NavigationView
without ToolBar (with ActionBar). In this activity I implement the onNavigationItemSelected
that I use to navigate to SettingsFragment
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
// more code
getSupportFragmentManager().beginTransaction().replace(R.id.container,new DashboardActivity()).commit();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// more code
getSupportFragmentManager().beginTransaction().replace(R.id.container,new SettingsFragment()).commit();
}
In the SettingsFragment
fragment I have a listener to open a new activity:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.activity_settings, container, false);
FloatingActionButton fab = (FloatingActionButton) fragmentView.findViewById(R.id.myfab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SubSettingsActivity.class);
fragmentView.getContext().startActivity(intent);
startActivity(intent);
}
});
return fragmentView;
}
It opens an activity but without a back button to the previous fragment. What's the proper way to add this button in the action bar?
EDIT: I would like the button to go back to the previous fragment SettingsFragment
and not to the activity that holds the fragments. Why implementing NavigationView
is so hard?
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();
}