Android development page jump, jump with parameters, Activity transfer data to Fragment

1. Page jump

Two parameters are required, the first LoginActivity is the current class, and the other MainActivity is the target class of the jump

 Intent intent = new Intent();
intent.setClass(LoginActivity.this,MainActivity.class);
startActivity(intent);

2. Carrying parameters

Use the putExtra method, which carries the key-value pair, the second line of code below

Intent intent = new Intent();
intent.putExtra("userName", DataMap.get("name"));
intent.setClass(LoginActivity.this,MainActivity.class);
startActivity(intent);

3. Activity passes data to Fragment

In Activity (Look at 1, 2 above, data progression relationship)

        Intent intent=getIntent();
        String userName=intent.getStringExtra("userName");
        Bundle bundle = new Bundle();
        bundle.putString("userName",userName);
        userFragment.setArguments(bundle);//数据传递到fragment中

In Fragment

        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        String userName = null;
        if(bundle!=null){
            userName = bundle.getString("userName");
            System.out.println(userName);
        }

Guess you like

Origin blog.csdn.net/qq_41170600/article/details/108903783