Custom method to optimize the value passed when the page jumps

In real development, different pages may be implemented by different people, and your data needs the data of the previous page. At this time, you can ask your colleague what the parameter name you passed over is, of course, there are more A good way is as follows:

1. Add a custom method for page jumping on your event page:

public class SecondActivity extends AppCompatActivity {

    private static final String TAG = "SecondActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //接收上一个活动发送过来的信息
        Intent intent = getIntent();
        Log.e(TAG, "onCreate: "+intent.getStringExtra("hello"));

    }
    //这就是自定义的方法,仔细看,他和实现页面跳转的代码几乎一样,就是context代替了明确的活动名
    public static void actionStart(Context context ,String data1){

        Intent intent = new Intent(context,SecondActivity.class);
        intent.putExtra("hello",data1);
        context.startActivity(intent);
    }
}

2. Tell your colleagues that when you start your activity, just call the actionStart() method:

public class FirstActivity extends AppCompatActivity {

    private Button gotoSecondBtn;
    private static  final String TAG = "FirstActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        //Log.e(TAG, "onCreate: "+getTaskId());
        gotoSecondBtn = findViewById(R.id.goto_second_btn);
        //对按钮进行监听
        gotoSecondBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              SecondActivity.actionStart(FirstActivity.this,"hello SecondActivity");
            }
        });
    }


}
Effect display:

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325541651&siteId=291194637