Android Probleme mit Navigation-Fragments?

jackie05

Mitglied
Hallo,
ich versuche schon seit einiger Zeit die Fragments von meiner Navigation, einer Variable zuzuweisen, leider ohne erfolg.
Das Fragment ist hier definiert:
Java:
<?xml version="1.0" encoding="utf-8"?>
<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/nav_cat">

    <fragment
        android:id="@+id/nav_cat"
        android:name="de.test.CatFragment"
        android:label="@string/menu_cat"
        tools:layout="@layout/fragment_cat" />
</navigation>
und hier ist die MainActivity.java Datei:
Java:
public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private Realm realm;
    private CatFragment catFragment;

    public CatFragment getCatFragment() {
        return catFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
      
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_cat)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        //Hier soll das Fragment in catFragment zugewiesen werden----------<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        catFragment = (CatFragment) getSupportFragmentManager().findFragmentById(R.id.nav_cat);

        if(catFragment != null){
            Toast.makeText(MainActivity.this, "Gefunden", Toast.LENGTH_LONG).show();
            catFragment.test();
        } else {
            Toast.makeText(MainActivity.this, "Nicht gefunden", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}
Das klappt einfach nicht.

Das Fragment wird in meiner App angezeigt, nur möchte ich dieses Fragment auch in einer Variable zuweisen, um auf dieses Fragment später zuzugreifen, um weitere Aktionen durchführen zu können.

Hier versuche ich das Fragment ausfindig zu machen:
Java:
catFragment = (CatFragment) getSupportFragmentManager().findFragmentById(R.id.nav_cat);
if(catFragment != null){
    Toast.makeText(MainActivity.this, "Gefunden", Toast.LENGTH_LONG).show();
    catFragment.test();
} else {
    Toast.makeText(MainActivity.this, "Nicht gefunden", Toast.LENGTH_LONG).show();
}
Leider ist catFragment immer null.

Was habe ich falsch gemacht?
Wie könnte ich am einfachsten das Fragment in einer Variable speichern, um über diese Variable dann auf das Fragment zuzugreifen?

Ich bedanke mich schon mal für die Hilfe.

Gruss
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
J Android Probleme mit BLE-Notify Android & Cross-Platform Mobile Apps 2
J Android Probleme mit der Realm Datenbank Android & Cross-Platform Mobile Apps 3
N Probleme mit custom dynamic ListView Android & Cross-Platform Mobile Apps 15
B Android Probleme mit Android Studio Android & Cross-Platform Mobile Apps 6
B Android Probleme mit Realm Datenbank Android & Cross-Platform Mobile Apps 2
B Android Probleme mit ArrayList Android & Cross-Platform Mobile Apps 6
J Android Probleme mit FileProvider Android & Cross-Platform Mobile Apps 1
B Probleme mit Firebase Authentication Android & Cross-Platform Mobile Apps 25
H Android Probleme mit SearchView in ArrayAdapter Android & Cross-Platform Mobile Apps 7
ATZENPOWER Android Probleme mit mobilen Daten via lte Android & Cross-Platform Mobile Apps 10
S Android Probleme beim Verbinden mit einer HTTPS Seite Android & Cross-Platform Mobile Apps 4
B Android Probleme mit RealmObject? Android & Cross-Platform Mobile Apps 1
M Android ExpandableListView merkwürdige Probleme Android & Cross-Platform Mobile Apps 20
F Probleme mit Google-Maps Android & Cross-Platform Mobile Apps 0
B Android Probleme mit ViewPager? Android & Cross-Platform Mobile Apps 5
J Probleme mit ViewPager und Activity Android & Cross-Platform Mobile Apps 1
B Android Probleme mit Eclipse? Android & Cross-Platform Mobile Apps 6
E MAVLINK Probleme Android & Cross-Platform Mobile Apps 1
C Android Probleme mit JavaMail Android & Cross-Platform Mobile Apps 5
B Android Probleme mit Facebook-SDK? Android & Cross-Platform Mobile Apps 1
D Android Probleme mit info/warning (1, 902) Android & Cross-Platform Mobile Apps 4
D Android Gallery Probleme Android & Cross-Platform Mobile Apps 3
B Probleme mit App auf Galaxy S3? Android & Cross-Platform Mobile Apps 13
S Hat der AVD-Manager Probleme mit GPS? Android & Cross-Platform Mobile Apps 5
P Probleme mit xml-Layout Android & Cross-Platform Mobile Apps 2
P Android Probleme mit Spinner Android & Cross-Platform Mobile Apps 3
F Layout mit listViews (Scrolling-Probleme) Android & Cross-Platform Mobile Apps 2
A Android Probleme mit Dialog Android & Cross-Platform Mobile Apps 4
U SQLite-Datenbank Probleme Android & Cross-Platform Mobile Apps 8
T Android Probleme bei Facebook Integration Android & Cross-Platform Mobile Apps 5
tfa Android Layout-Probleme: View programmatisch erweitern (addContentView) Android & Cross-Platform Mobile Apps 7
A Probleme mit ListView / ArrayAdapter Android & Cross-Platform Mobile Apps 3
A Probleme mit Form.isShown Android & Cross-Platform Mobile Apps 9
A Probleme mit Calendar auf dem Handy Android & Cross-Platform Mobile Apps 3
U Probleme mit der drawString Methode bei Canvas Android & Cross-Platform Mobile Apps 8
P Probleme mit dem Deployment Android & Cross-Platform Mobile Apps 3
P Probleme mit Streams Android & Cross-Platform Mobile Apps 4
C 2 kleine Probleme (Datei lesen, String durchsuchen) Android & Cross-Platform Mobile Apps 16
G Proguard Obfuscator macht Probleme Android & Cross-Platform Mobile Apps 2
P Probleme mit RMS Android & Cross-Platform Mobile Apps 5
W Navigation MenuItem Switch Fehler Android & Cross-Platform Mobile Apps 6
OSchriever Navigation drawer Strings ändern sich nicht Android & Cross-Platform Mobile Apps 0
K Android Datenaustausch zwischen zwei Fragments Android & Cross-Platform Mobile Apps 1
W Android Designfrage / Layout / Activity / Fragments Android & Cross-Platform Mobile Apps 2
J Liste in Tab mit Fragments Android & Cross-Platform Mobile Apps 0
N Viewpager Fragments replace Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben