简体   繁体   中英

How to avoid skipping of activities when using multiple intents?

I am trying to pass a user input value from fourth activity to fifth as well as sixth activity. I have used intents for passing the value.But now When i run the app, it jumps from fourth to sixth activity on button click,skipping the fifth activity. Is it because I have used both the intents together? How can i modify the code to avoid this?

Fourth.java

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fourth);

    final EditText et;
    final Button b;

    et = (EditText) findViewById(R.id.editText1);
    b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
           Intent intent = new Intent(Fourth.this, Fifth.class);
           intent.putExtra("thetext", et.getText().toString());
           startActivity(intent);

           Intent intentnew = new Intent(Fourth.this, Sixth.class);
           intentnew.putExtra("thetext", et.getText().toString());            
           startActivity(intentnew);   
        }
    }
}

Here are some of the options you can have.

  1. As you want to use Intent only at this moment, so you can pass the data of your fourth activity to fifth using Intent .And then again pass the same data from fifth to sixth using another Intent from the fifth activity.

so in your fourth activity have this

Intent intent = new Intent(Fourth.this, Fifth.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent); 

And in your fifth,

String text = getIntent().getStringExtra("thetext");
Intent intentnew = new Intent(Fifth.this, Sixth.class);
intentnew.putExtra("theSametext", text);            
startActivity(intentnew);

2.You can Save data to SharedPreferences - Using this you save the information to the "preference" file of the application. Refer this question and answer to learn how to use it.

3.Write it to SQLite Database - You can create aa new SQLite table for storing data and write new rows to it. This has way more overhead and is really only useful if you have significant amount of data to be stored in the same application.You can refer this tutorial for this.

4.Also you can create a Singelton class which can be a static class with public properties that can be set.However this could be useful only for temporary creation and retention of data across many activities.

So if you want to do it using Intent only you can use the first method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM