How do I get extra data from intent on Android?

后端 未结 16 2280
清歌不尽
清歌不尽 2020-11-21 11:01

How can I send data from one activity (intent) to another?

I use this code to send data:

Intent i=new Intent(context,SendMessage.class);
i.putExtra(\         


        
相关标签:
16条回答
  • 2020-11-21 12:00

    You can also do like this
    // put value in intent

        Intent in = new Intent(MainActivity.this, Booked.class);
        in.putExtra("filter", "Booked");
        startActivity(in);
    

    // get value from intent

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String filter = bundle.getString("filter");
    
    0 讨论(0)
  • 2020-11-21 12:01

    If used in a FragmentActivity, try this:

    The first page extends FragmentActivity

    Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
    Tabdetail.putExtra("Marker", marker.getTitle().toString());
    startActivity(Tabdetail);
    

    In the fragment, you just need to call getActivity() first,

    The second page extends Fragment:

    String receive = getActivity().getIntent().getExtras().getString("name");
    
    0 讨论(0)
  • 2020-11-21 12:02

    Instead of initializing another new Intent to receive the data, just do this:

    String id = getIntent().getStringExtra("id");
    
    0 讨论(0)
  • 2020-11-21 12:02

    Just a suggestion:

    Instead of using "id" or "name" in your i.putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent.EXTRA_something.

    A full list can be found at Intent (Android Developers).

    0 讨论(0)
提交回复
热议问题