Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How to add a back button to the fragment in the action bar of an opened activity?

Posted on November 11, 2022 By

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:
See Screenshot

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
enter image description here

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!

READ  [FIXED] App Action Test Tools does not work on Android Studio RC or Canary
Powered by Inline Related Posts

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();

}

Check this video for more

Problem :

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?

Comments

Comment posted by vesii

But how to make it go the the fragment

READ  [FIXED] java - What is the VINTF manifest in android studio? Have an error
Powered by Inline Related Posts

Comment posted by vesii

Thanks for the suggestions but I just migrated to this way so I prefer to stick with it.

Comment posted by Jason

Once you see how simple it is, you’ll love it

Comment posted by vesii

As there are no other answer to this issue, can you please expand your answer about Android Jetpack navigation component? Is it available for API 16? How should I add it to my code? I read the docs and the example there looks different from what I need.

Comment posted by Jason

@vesii can u mark as correct answer if it helps you

Android Tags:android, android-layout, java, navigationview

Post navigation

Previous Post: [FIXED] java – How can I solve this Android Studio Firebase problem?
Next Post: [FIXED] Android Java Publish result cast issue at MyCustomAdapter.java

Related Posts

[FIXED] android – Cloud Firestore: does updating the same document twice in a batch count as a single write? Android
[FIXED] android – Is it possible to detect if an app in AppStore is developed with React Native? Android
[FIXED] kotlin – How can i make this animation stop after completion in Jetpack Compose or not be infinite Android
[FIXED] java – Why does my Android Dialog throw a NullPointer? Android
[FIXED] Toggle method not working properly in Android Studio using JAVA Android
[FIXED] android – How do I replace AsyncTask with RxJava in Room methods? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme