Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check reason for onResume() method

I need to check whether the onResume() is called because the activity starts for the first time (1st case) or it's called because my result after using startActivityForResult is back (2nd case) ..

I need to know because I need to scroll to a specific view if the case is case 2 and to start the activity normally if the case is case 1..

So what is the if condition I should use ?

like image 1000
Ahmad Sayed Abdulrahman Avatar asked Oct 17 '22 08:10

Ahmad Sayed Abdulrahman


2 Answers

@rafsanahmad007's answer is good, but in case you really do need to use onResume(), there are a few ways to approach it.

One option is to set a boolean flag inside onActivityResult() and then check it in onResume():

private boolean returningFromResult = false;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == FOO) {
        this.returningFromResult = true;
        // ...
    }
}

@Override
protected void onResume() {
    super.onResume();

    if (returningFromResult) {
        // special case
    } else {
        // normal case
    }
}

Another option is to check inside onCreate() whether the savedInstanceState parameter is null. If it's null, that means that you're starting up for the first time. You can then combine this with logic in onResume() to determine if onResume() has been called more than once:

private boolean isFirstOnResume;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.isFirstOnResume = (savedInstanceState == null);

    // ...
}

@Override
protected void onResume() {
    super.onResume();

    if (isFirstOnResume) {
        this.isFirstOnResume = false;
        // normal case
    } else {
        // special case
    }
}

Still, if you can simply use onActivityResult() to perform your logic, that will be the best way to go.

like image 136
Ben P. Avatar answered Oct 30 '22 03:10

Ben P.


If you are using startActivityForResult() then there is no need to listen for onResume() to be called. It has a callback method where you can write the scroll code based on your condition.

For example:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1); //here 1 is the request code

In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.

For example: In SecondActivity:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK,returnIntent);
finish();

Now in your FirstActivity class write following code for the onActivityResult() method.

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

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            //case 2
            //Write your code
        }
    }
}
like image 33
rafsanahmad007 Avatar answered Oct 30 '22 05:10

rafsanahmad007