简体   繁体   中英

Android return to app call method

I just recently started learning how to build android apps, and encountered a problem: I want, when users leave the app (go to the homescreen, multitask), and they return, that the app calls a certain method. How can I do that?

This problem is more tricky than it may look like. When you return to app after leaving it, then is called method onResume of activity which was active when app was interrupted. But same happens when you go from one activity to another (onResume of second activity is called). If you just call method from onResume, it will be called every time onResume of any activity is called.

Take a look at this solution...

First, you have BaseActivity which is extended by all activities that need to call that method:

abstract public class BaseActivity extends Activity implements IName {

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

    @Override
    protected void onResume() {
        if (AppClass.getPausedActivity() != null) {
            if (this.getClassName().equals(AppClass.getPausedActivity()))
                //call specific method
        }

        AppClass.setPausedActivity(""); 
        super.onResume();
    }

    @Override
    protected void onPause() {
         AppClass.setPausedActivity(this.getClassName());
         super.onPause();
    }

    @Override
    abstract public String getClassName();
}

As you can see it implements interface IName:

public interface IName
{
    String getClassName();
}

BaseActivity in onPause (when it is interrupted) calls setPausedActivity method of AppClass which remembers last activity name that was interrupted. In onResume (when app and activity is continued) we compare name of current activity and last paused activity.

So, when app is interrupted, these names will be same because you paused one activity and you got back to the same one. When you call activity from some other activity these names will not be same and method will not be called.

Here is code for AppClass:

public class AppClass extends Application {

    public static String pausedActivity;

    public static String getPausedActivity() {
        return pausedActivity;
    }

    public static void setPausedActivity(String _pausedActivity) {
        pausedActivity = _pausedActivity;
    }
}

Also, here is example of activity that extends BaseActivity:

public class MainActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
    }

    //here you set name of current activity
    @Override
    public String getClassName() {
        return "MainActivity";
    }
}

You are bound to the Activity lifecycle . You will need to implement corresponding logic to figure out if the user has been in your app before (ie using SharedPreferences ).

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