how to directly pass value of a variable from 1st

2019-03-02 04:58发布

问题:

how to directly pass value of a variable from 1st activity to 3rd activity using putextra?

For example:

I have a variable A in the first screen (first activity) if I hit the button it will show the 2nd screen. The 2nd screen(2nd activity) has a button. If I click the button it will open the 3rd screen(3rd activity) and it will show the variable A in the textview of 3rd screen (3rd activity).

how can I pass the value of variable A from the first screen DIRECTLY to the third screen(third activity). (1st->3rd)

is it possible to directly pass the value ? or should I pass it in 2nd activity first before passing to 3rd activity (1st->2nd->3rd)

回答1:

Actually its not possible to go from 1st activity to 3rd directly...

You must have to follow the activity stack..

So if you want the data to be passed from 1st to 3rd follow by

1st -> 2nd -> 3rd

with the use of intent suggested by @Lalit Poptani



回答2:

You can pass any value between any two Activities you want. You just need to do two things:

  • In your FirstActivity:

    Intent intent = new Intent(context, ThirdActivity.class);
    i.putExtra("value_key", value); //valus is a String
    startActivity(intent);
    
  • In your ThirdActivity's onCreate():

    Bundle b = getIntent().getExtras();
    String value = (String) b.getString("value_key");
    


回答3:

You may keep them as static objects and retrieve as Myclass.myObject. But what you really want is impossible.



回答4:

You can use a database, SharedPreferences, Application, Static Methods and more for share data between Activitys. Look here