简体   繁体   English

来自活动的putExtra然后是片段中的getExtra

[英]putExtra from activity then getExtra in fragment

i tried to put and get extra from activity to fragment . 我尝试从活动中提取额外的钱。 but something is wrong! 但是出事了! anybody have idea? 有人有主意吗? my case is diffrent because i wanna do it in fragment 我的案子不一样,因为我想零碎地做

myActivity : myActivity:

if(email.matches(users.user1)&&password.matches(users.pass1)){
        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
        Intent i = new Intent(LoginActivity.this,ProfileFragment.class);
        i.putExtra("pn", users.pn1);
        i.putExtra("name", users.name1);
        i.putExtra("family", users.family1);
        i.putExtra("rank", users.rank1);
        startActivity(intent);
        finish();
    }

myfragment Myfragment

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_profile2, container, false);

    final TextView pn =getActivity().findViewById(R.id.pn);
    final TextView name =getActivity().findViewById(R.id.name);
    final TextView family =getActivity().findViewById(R.id.family);
    final TextView user =getActivity().findViewById(R.id.user);
    final TextView rank =getActivity().findViewById(R.id.rank);

    String pnget = getActivity().getIntent().getStringExtra("pn");
    String nameget = getActivity().getIntent().getStringExtra("name");
    String familyget = getActivity().getIntent().getStringExtra("family");
    String userget = getActivity().getIntent().getStringExtra("user");
    String rankget = getActivity().getIntent().getStringExtra("rank");

    pn.setText(pnget);
    name.setText(nameget);
    family.setText(familyget);
    user.setText(userget);
    rank.setText(rankget);
}

Hi . 你好 i tried to put and get extra from activity to fragment . 我尝试从活动中提取额外的钱。 but something is wrong! 但是出事了! anybody have idea? 有人有主意吗?

You start an intent call intent. 您启动一个意图调用意图。 But the intent have data is i, and it have't started yet. 但意图是要获取数据,但尚未开始。

You can use setArgument and getArgument to send and receive data from activity to fragment or from fragment to fragment: 您可以使用setArgument和getArgument从活动到片段或从片段到片段发送和接收数据:

In YourReceiveFragment: 在YourReceiveFragment中:

public static Fragment newInstance(String data1, String data2, ...) {
    Fragment f = new YourReceiveFragment();
    Bundle bundle = new Bundle();
    bundle.putString(DATA_RECEIVE1, data1);
    bundle.putString(DATA_RECEIVE2, data2);
    f.setArguments(bundle);
    return f;
}

In your activity: Just call it: 在您的活动中:只需调用它:

Fragment f = YourReceiveFragment.newInstance(yourString1, yourString2);

                            FragmentTransaction ft = getFragmentManager().beginTransaction();
                            ft.add(R.id.main_container, f).commit();

Then in YourReceiveFragment: 然后在YourReceiveFragment中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null){
        String dataReceive1 = getArguments().getString(DATA_RECEIVE1);
        String dataReceive2 = getArguments().getString(DATA_RECEIVE2);
    }
}

In case you want to send data from an activity to a fragment in another activity, use interface or an easier way is just pass the data to the activity which contains fragment and from that, send data to fragment. 如果您想将数据从一个活动发送到另一个活动中的片段,则使用接口或一种更简单的方法是将数据传递到包含片段的活动中,然后再将数据发送到片段中。

You can send data with the bundle, that is recommended 您可以使用捆绑包发送数据,建议您

To Pass Data from Activity 从活动传递数据

fragment = new YourFragment();

if (fragment != null) {
    FragmentManager fragmentManager = getFragmentManager();

    Bundle bundle = new Bundle();
    bundle.putString("key", "value");

    fragment.setArguments(bundle);

    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}

To receive data from Fragment 从Fragment接收数据

  String var = getArguments().getString("value");

You can't create a Fragment with startActivity. 无法使用startActivity创建一个Fragment。 You need to create the fragment with bundle like this: 您需要使用bundle创建片段,如下所示:

ProfileFragment fragment = new ProfileFragment();
Bundle args = new Bundle();

args.putString("name", users.name1);
args.putString("family", users.family1);
fragment.setArguments(args);

// then tell the FragmentManager to attach the fragment
// to the activity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.your_placeholder, fragment);
ft.commit();

Then in your onCreateView get them with: 然后在您的onCreateView获取它们:

String name = getArguments().getString("name", ""); 
String family = getArguments().getString("family", ""); 

Please remember that you need to move the return code to the last of onCreateView method and change your code to something like this: 请记住,您需要将返回代码移至onCreateView方法的最后一个并将代码更改为如下所示:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.fragment_profile2, container, false);

   TextView pn =view.findViewById(R.id.pn);
   ...

   return view;
}

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

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