pass a string from first activity to last activity but running another activity before starting last activity

1367 views java
0

I'm very new in android, if the question is repeated please avoid and send the link. There are three activities A, B and C. Activity A gets a user name which i want to get displayed in activity C,but i want to run Activity B first and then run Activity C. The problem using intent is that i have to run C first. If singleton,bundle, or parcelable is the solution can you please provide the code?

Activity A

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText nameText = findViewById(R.id.nameText);
    nameText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            String name = nameText.getText().toString();
            return true;
        }
    });
}

Activity B

    public class qPage2 extends AppCompatActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_q_page2);
                android.os.SystemClock.sleep(500);
            }
        }

Activity C

public class lastPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_last_page);
    }
}

answered question

1 Answer

12

There are many options for this : 1. You can use intent as you mentioned 2. You can make a static Class and declare a static variable with type string and access it in the C activity .

Code For first option : For adding value to intent Intent n = new Intent(FirstActivity.class,SecondActivity.class); n.putExtra("name" , name); n.startActivity();

For retrieving it :

Intent intent = getIntent(); String name = intent.getStringExtra("name");

2.Create a static class member and acess it

class static StaticClass{
     static name = " ";
 }

Update this variable in the first activity and retrive it in last activity

posted this

Have an answer?

JD

Please login first before posting an answer.