0

I have an Activity A, I want to start Activity B to load its data from my server and when all the data is retrieved, I want to finish Activity A so that B becomes on top of my stack and be displayed

I cannot solve this problem, I have no idea how can I achieve this

Android
  • 1,420
  • 4
  • 13
  • 23
Ahmed Khairy
  • 145
  • 2
  • 16

4 Answers4

0

You could load the data in Activity A and when it's done loading start Activity B and pass the data between the activities, you can see how to do it here

Farez
  • 1
0

In your Activity A do like this -

    // 1. create an intent pass class name or intnet action name 
    Intent intent = new Intent(this,AnotherActivity.class);

    // 2. put X, Y in intent
    intent.putExtra("x",  etX.getText().toString());
    intent.putExtra("y",  etY.getText().toString());

    // 3. start the activity
    startActivityForResult(intent, 1);

In your activity B do like this -

public class ActivityB extends Activity implements OnClickListener {

    TextView tvSum;
    Button btnSendResult;
    int result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

// 1. get passed intent 
    Intent intent = getIntent();

    // 2. get x & y from intent
    int x = Integer.parseInt(intent.getStringExtra("x"));
    int y = Integer.parseInt(intent.getStringExtra("y"));
    result = x + y;

    btnSendResult = (Button) findViewById(R.id.btnSendResult);
    btnSendResult.setOnClickListener(this);
}

Then outside onCreate in activity B -

@Override
    public void onClick(View view) {

         Intent returnIntent = new Intent();
         returnIntent.putExtra("result",result);
         setResult(RESULT_OK,returnIntent);     
         finish();
    }
Android
  • 1,420
  • 4
  • 13
  • 23
Anupam
  • 2,845
  • 2
  • 16
  • 30
0

Use Broadcast receiver in Activity A and start listening to the broadcast when we start the Activity B. After loading the data in the Activity B, Send the broadcast. After receiving the broadcast, finish() the activity A.

Milan Joseph
  • 118
  • 10
0

You could load the data using job scheduler from Activity A, once data has been loading start Activity B and pass the data.

Sanjay Bhalani
  • 2,424
  • 18
  • 44