How to pass value from 1st activity to 3rd activity

前端 未结 7 1980
暖寄归人
暖寄归人 2020-12-12 07:25

I wish to pass the value from 1st activity to 3rd activity.

My 1st activity: CustomizedListview

2nd activity is: SingleMenuItemActivity

3rd activity

相关标签:
7条回答
  • 2020-12-12 08:01

    Use SharedPreferences (for small data)to store the data and get this data in you 3 activity otherwise use internal storage or database(for large data)

    0 讨论(0)
  • 2020-12-12 08:04
    //your 1st activity (CustomizedListview)
                 
    String str=txtView.getText().toString();
    
    Intent i=new Intent(getApplicationContext(),SingleMenuItemActivity.class);
    i.putExtra("message",str);
    
    startActivity(i);
    
        //your 2nd Activity (SingleMenuItemActivity)
    
    
    String str= getIntent().getStringExtra("message");
    
    Intent i = new Intent(getApplicationContext(), InsertionExample.class);
    
    i.putExtra("message", str);
    
    startActivity(i);
    
        //you 3rd Activity (InsertionExample)
    
    
    Intent int=getIntent();
    
    String str= int.getStringExtra("message");
    
    txtView.setText(str);
    
    0 讨论(0)
  • 2020-12-12 08:08

    I have to passed orderid value from 1st activity to second activity

    Send as you send to second activity . Just change name of second activity to third activity.

    Or

    Just Store Order Id in Shared Preference and get it in third Activity.

    SharedPrefernce Example

    SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    SharedPreferences.Editor prefsEditor = myPrefs.edit();
    prefsEditor.putString("order_id", "5");
    prefsEditor.commit();
    

    Get Shared Preference.

     SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
     String prefName = myPrefs.getString("order_id", "0");
    
    0 讨论(0)
  • 2020-12-12 08:08

    Make the value static, and then use it in the 3rd activity

    public static int i;
    

    And then, call it on the 3rd activity:

    firstActivity.i;
    
    0 讨论(0)
  • 2020-12-12 08:11

    You can use intent extra from first activity to second and then pass the same value by extra in another intent from secon to third.

    0 讨论(0)
  • 2020-12-12 08:13

    You can pass the value in 2 ways:

    1. Either you make a global Class and set the value in that class and access that class in your 3rd activity
    2. You can use Intent to send your values from 1st activity to 2nd activity

    Intent intent = new Intent (this, 2ndActivity.class); 
        intent.putExtra ("Value",Value);
        startActivity(intent);
    

    And the same you can do for 2nd activity to 3rd activity

    Bundle extras = getIntent().getExtras();
        if(extras!=null){
        Values=extras.getString("value");
        }
    
    0 讨论(0)
提交回复
热议问题