简体   繁体   English

如何在 android 中的片段和活动之间共享数据

[英]How to share data between fragments and activities in android

I am new to android.我是 android 的新手。
Can anyone please tell me if it is possible to share data between activities and fragments using java interfaces.谁能告诉我是否可以使用 java 接口在活动和片段之间共享数据。 I have studied OOP but I am still stuck in interfaces and abstracts classes.我研究过 OOP 但我仍然停留在接口和抽象类中。 I think if I implement a class on many activities, I will be able to share data like passing data from one activity and getting it from another.我认为,如果我在许多活动中实现 class,我将能够共享数据,例如从一个活动传递数据并从另一个活动获取数据。
Am I right about it?我说得对吗? Please help me请帮我

For between activities you can putExtra values such as below:对于活动之间,您可以放置额外的值,如下所示:

Intent intent = new Intent (this, newActivity.class);
intent.putExtra("someKey", someValue);
intent.putExtra(bundle);
startActivity(intent);

To get it inside your activity:要将其放入您的活动中:

getIntent().getExtra("someKey");

For moving values between fragments i'd suggest using bundles:对于在片段之间移动值,我建议使用捆绑包:

//Where mainlayout is the top level id of your xml layout and R.id.viewProfile is the id of the action within your navigation xml.
       NavController navController = Navigation.findNavController(getActivity(), R.id.mainlayout);
                                    Bundle bundle = new Bundle();
                                    bundle.putString("uid",snapshot.getKey());
                                    navController.navigate(R.id.viewProfile,bundle);

to retrieve this value within your fragment:在您的片段中检索此值:

String game = getArguments().getString("game");

Hopefully that helps.希望这会有所帮助。

use intents and them use the putExtra() and getExtra() to pass and recieve information, alternatively you can pass them as NavArgs() when using the jetpack navigation library.使用意图,它们使用 putExtra() 和 getExtra() 来传递和接收信息,或者您可以在使用喷气背包导航库时将它们作为 NavArgs() 传递。

intents in javajava 中的意图

navigation 导航

Android has standards for setting and getting data in activities and fragments Android 有在活动和片段中设置和获取数据的标准

To send data between activities在活动之间发送数据

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("YOUR_INT", 1);
b.putString("YOUR_STRING", "Hello");
intent.putExtras(b);
startActivity(intent);

To send data to a fragment from an activity从活动向片段发送数据

// Declare a static method on your fragment 'New Instance'
public static MyFragment newInstance(int yourInt, String yourString) {
    MyFragment myFragment = new MyFragment();

    Bundle args = new Bundle();
    args.putInt("YOUR_INT_KEY", yourInt);
    args.putString("YOUR_STRING_KEY", yourString);
    myFragment.setArguments(args);

    return myFragment;
}

// Get the data inside your fragment
getArguments().getInt("YOUR_INT_KEY", 0); //Default value is zero if no int was found


// Instantiate your fragment wherever
MyFragment f = MyFragment.newInstance(1, "Hello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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