简体   繁体   English

Android返回应用程序调用方法

[英]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. 我最近才开始学习如何构建android应用程序,遇到一个问题:我希望当用户离开应用程序(转到主屏幕,执行多任务)并返回时,该应用程序调用某种方法。 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. 当您离开应用程序后返回应用程序时,则称为onResume活动方法,该方法在应用程序被中断时处于活动状态。 But same happens when you go from one activity to another (onResume of second activity is called). 但是,当您从一项活动转到另一项活动时(调用第二项活动的onResume),也会发生同样的情况。 If you just call method from onResume, it will be called every time onResume of any activity is called. 如果仅从onResume调用方法,则每次调用任何活动的onResume时都会调用该方法。

Take a look at this solution... 看看这个解决方案...

First, you have BaseActivity which is extended by all activities that need to call that method: 首先,您拥有BaseActivity,该扩展被需要调用该方法的所有活动扩展:

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: 如您所见,它实现了接口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. onPause中的BaseActivity(被中断时)调用AppClass的setPausedActivity方法,该方法记住被中断的上一个活动名称。 In onResume (when app and activity is continued) we compare name of current activity and last paused activity. 在onResume中(当应用程序和活动继续时),我们比较当前活动和上次暂停的活动的名称。

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: 这是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: 另外,这是扩展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 . 您必须遵守Activity 生命周期 You will need to implement corresponding logic to figure out if the user has been in your app before (ie using SharedPreferences ). 您将需要实现相应的逻辑来确定用户之前是否曾经在您的应用程序中使用过(即使用SharedPreferences )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM