SpeedEX505
SpeedEX505

Reputation: 2076

startActivity in onCreate()

I need to start Activity in onCreate of another Activity and wait until activity2 finish. How to do that?

public class Activity1 extends Activity{
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        //do some code
        startActivity(Activity2)
        //wait until activity2 finish
        //another code which can be done after activity2 finish
    }
 ...
 }

Or I need to do another code in OnActivityResult async way??

Upvotes: 1

Views: 6796

Answers (5)

Amit Kumar Khare
Amit Kumar Khare

Reputation: 573

You should try this way...

public class FirstActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = new Intent(SecondActivity.this, getApplicationContext());

        int requestCode = 0;
        startActivityForResult(i,requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

       // do your stuff here after SecondActivity finished.
    }
}

Hope this helps you.

Upvotes: 3

Cheerag
Cheerag

Reputation: 435

You can start activity2 like startActivityForResult(intent, requestCode).

And do what ever you want to do in onActivityResult(int arg0, int arg1, Intent arg2) on finsh activity2.

Upvotes: 1

mostafa_zakaria
mostafa_zakaria

Reputation: 116

You can use startActivtyForResult method to start the activty2 then when onActivityResult of activty1 called call setContentView for the first one

Upvotes: 2

Anuj
Anuj

Reputation: 476

You should use startActivityForResult(intent, requestCode);

Upvotes: 1

Adam Radomski
Adam Radomski

Reputation: 2575

You can use startActivityForResult and then catch the result in OnActivityResult

Doc

Upvotes: 1

Related Questions