Ich habe die Navigationsarchitekturkomponente ausprobiert und habe jetzt Schwierigkeiten beim Festlegen des Titels. Wie setze ich den Titel programmgesteuert und wie funktioniert es?
Um meine Frage zu klären, lassen Sie uns ein Beispiel haben, in dem ich eine einfache App mit eingerichtet habe MainActivity
Hosting des Navigations-Host-Controllers, der MainFragment
hat eine Schaltfläche und beim Klicken auf die Schaltfläche geht es zu DetailFragment
.
Derselbe Code aus einer anderen Frage mehrerer App-Leisten bei Stapelüberlauf.
Hauptaktivität
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting up a back button
NavController navController = Navigation.findNavController(this, R.id.nav_host);
NavigationUI.setupActionBarWithNavController(this, navController);
}
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.nav_host).navigateUp();
}
}
HauptFragment
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button buttonOne = view.findViewById(R.id.button_one);
buttonOne.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.detailFragment));
}
}
DetailFragment
public class DetailFragment extends Fragment {
public DetailFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail, container, false);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_marginTop="?android:attr/actionBarSize"
app:defaultNavHost="true"
app:layout_anchor="@id/bottom_appbar"
app:layout_anchorGravity="top"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_gravity="bottom" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottom_appbar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
navigation.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.example.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/toAccountFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="com.example.DetailFragment"
android:label="fragment_account"
tools:layout="@layout/fragment_detail" />
</navigation>
Wenn Sie also meine App starten, der Titel ist “MainActivity”. Wie üblich zeigt es die MainFragment
das die Schaltfläche enthält, zu der man gehen soll DetailFragment
. In dem DialogFragment
Ich habe den Titel wie folgt festgelegt:
getActivity().getSupportActionBar().setTitle("Detail");
Erstes Problem: Klicken Sie also auf die Schaltfläche auf der MainFragment
zu gehen DetailFragment
, es geht dorthin und der Titel ändert sich zu “Detail”. Aber beim Klicken auf die Zurück-Schaltfläche, die Titel ändert sich zu “fragment_main”. Also habe ich diese Codezeile hinzugefügt MainFragment
:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// ...
//Showing the title
Navigation.findNavController(view)
.getCurrentDestination().setLabel("Hello");
}
Nun kehrt die zwar zurück aus DetailFragment
zu MainFragment
der Titel ändert sich in „Hallo“. Aber hier kommt die zweites Problemwenn ich die App schließe und neu starte, ändert sich der Titel wieder in „MainActivity“, obwohl es stattdessen „Hello“ anzeigen sollte, weißt du?
Ok, dann hinzufügen setTitle("Hello")
in MainFrgment
funktioniert auch nicht. Beispiel: Die Aktivität beginnt und der Titel lautet „Hallo“, gehen Sie zu DetailsFragment
und drücken Sie erneut die Zurück-Taste, geht der Titel zurück zu “fragment_main”.
Die einzige Lösung ist, beides zu haben setTitle("Hello")
zusammen mit Navigation.findNavController(view).getCurrentDestination().setLabel("Hello")
in MainFragment
.
Was ist also der richtige Weg, um den Titel für Fragmente mit der Navigationskomponente anzuzeigen?
möglicherweise dupliziert von stackoverflow.com/questions/50599238/…
– Agna JirKon Rx
25. Juli 2019 um 22:46 Uhr
Beantwortet das deine Frage? Dynamischer ActionBar-Titel aus einem Fragment mit AndroidX-Navigation
– Adam Hurwitz
14. Dezember 2020 um 18:15 Uhr