简体   繁体   中英

Pass arguments from fragment to another activity

I'm using the navigation controller for navigating. I need to navigate from the current fragment to a different activity and pass it some data. This has been problematic. Navigating to the activity works but the arguments are null. Here is the global action I am using in my navigation graph:

<action android:id="@+id/sign_out"
        app:destination="@id/login_activity">

    <argument
            android:name="clearSession"
            app:argType="boolean"
            android:defaultValue="true"/>

</action>

<activity android:id="@+id/login_activity"
          android:name="com.example.ui.login.LoginActivity"
          android:label="loginActivity">

    <argument
            android:name="clearSession"
            app:argType="boolean"
            android:defaultValue="true"/>

</activity>

And the menu navigation item:

<item android:title="@string/app">
    <menu>
        <group android:checkableBehavior="single">
            <item
                    android:id="@+id/sign_out"
                    android:icon="@drawable/ic_exit_to_app"
                    android:title="@string/sign_out"/>
        </group>
    </menu>
</item>

Code for setting up navigation:

navController = Navigation.findNavController(this, R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(setOf(R.id.connections_fragment, R.id.search_fragment), drawerLayout)

// Set up ActionBar
setSupportActionBar(binding.toolbar)
setupActionBarWithNavController(navController, appBarConfiguration)


// Set up navigation menu
binding.navigationView.setupWithNavController(navController)

I'm using a navigation drawer and a menu item in the navigation view has its id set to "sign_out". Clicking on this menu item will cause the Login screen to be loaded but the arguments are not provided even though they are specified in both the action and the destination. The navigation controller does not appear to be sending the arguments when it launches the Login activity and I suspect that this is a bug in Android. There are virtually no posts in Stackoverflow showing users passing data from a fragment to a different activity.

Try Like this. This way you can call Activity from Fragment :

public class FragmentName extends Fragment{

   OnCallbackReceived myCallback;
   //Interface
   public interface OnCallbackReceived {
   public void Update();
}

In Fragment :

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

try {
    myCallback= (OnCallbackReceived) activity;
} catch (Exception e) {}
}
// myCallback is the activity context.
// Call Events from Fragment 
myCallback.Update();

In Activity :

public class MainActivity extends Activity
    implements FragmentName .OnCallbackReceived {
public override void Update() {
    //Your Logic
}

Is your sign_out action inside <fragment> tag? If so you can call use navigate() method:

  val bundle = Bundle().apply { putString("EXTRA_KEY", "EXTRA_VALUE") }
  findNavController().navigate(R.id.sign_out, bundle)

And then val stringExtra = intent.getStringExtra("EXTRA_KEY") in LoginActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM