How do I get extra data from intent on Android?

后端 未结 16 2299
清歌不尽
清歌不尽 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 11:41

    Getting Different Types of Extra from Intent

    To access data from Intent you should know two things.

    • KEY
    • DataType of your data.

    There are different methods in Intent class to extract different kind of data types. It looks like this

    getIntent().XXXX(KEY) or intent.XXX(KEY);


    So if you know the datatype of your varibale which you set in otherActivity you can use the respective method.

    Example to retrieve String in your Activity from Intent

    String profileName = getIntent().getStringExtra("SomeKey");
    

    List of different variants of methods for different dataType

    You can see the list of available methods in Official Documentation of Intent.

    0 讨论(0)
  • 2020-11-21 11:42

    Add-up

    Set Data

    String value = "Hello World!";
    Intent intent = new Intent(getApplicationContext(), NewActivity.class);
    intent.putExtra("sample_name", value);
    startActivity(intent);
    

    Get Data

    String value;
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        value = bundle.getString("sample_name");
    }
    
    0 讨论(0)
  • 2020-11-21 11:42

    This is for adapter , for activity you just need to change mContext to your Activty name and for fragment you need to change mContext to getActivity()

     public static ArrayList<String> tags_array ;// static array list if you want to pass array data
    
          public void sendDataBundle(){
                tags_array = new ArrayList();
                tags_array.add("hashtag");//few array data
                tags_array.add("selling");
                tags_array.add("cityname");
                tags_array.add("more");
                tags_array.add("mobile");
                tags_array.add("android");
                tags_array.add("dress");
                Intent su = new Intent(mContext, ViewItemActivity.class);
                Bundle bun1 = new Bundle();
                bun1.putString("product_title","My Product Titile");
                bun1.putString("product_description", "My Product Discription");
                bun1.putString("category", "Product Category");
                bun1.putStringArrayList("hashtag", tags_array);//to pass array list 
                su.putExtras(bun1);
                mContext.startActivity(su);
            }
    
    0 讨论(0)
  • 2020-11-21 11:44

    Kotlin

    First Activity

    val intent = Intent(this, SecondActivity::class.java)
    intent.putExtra("key", "value")
    startActivity(intent)
    

    Second Activity

    val value = getIntent().getStringExtra("key")
    

    Suggestion

    Always put keys in constant file for more managed way.

    companion object {
        val PUT_EXTRA_USER = "PUT_EXTRA_USER"
    }
    
    0 讨论(0)
  • 2020-11-21 11:49

    You can get any type of extra data from intent, no matter if it's an object or string or any type of data.

    Bundle extra = getIntent().getExtras();
    
    if (extra != null){
        String str1 = (String) extra.get("obj"); // get a object
    
        String str2 =  extra.getString("string"); //get a string
    }
    

    and the Shortest solution is:

    Boolean isGranted = getIntent().getBooleanExtra("tag", false);
    
    0 讨论(0)
  • 2020-11-21 11:50

    Pass the intent with value on First Activity:

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("uid", uid.toString());
    intent.putExtra("pwd", pwd.toString());
    startActivity(intent);
    

    Receive intent on second Activity;-

    Intent intent = getIntent();
    String user = intent.getStringExtra("uid");
    String pass = intent.getStringExtra("pwd");
    

    We use generally two method in intent to send the value and to get the value. For sending the value we will use intent.putExtra("key", Value); and during receive intent on another activity we will use intent.getStringExtra("key"); to get the intent data as String or use some other available method to get other types of data (Integer, Boolean, etc.). The key may be any keyword to identify the value means that what value you are sharing. Hope it will work for you.

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