简体   繁体   English

按下后退按钮后立即拨打方法(Android)

[英]Make a method call as soon as back button has been pressed (Android)

In a given activity, an AlertDialog takes the user into WiFI settings. 在给定的活动中,AlertDialog将用户带入WiFI设置。 Then, the user presses the back button to return to said activity. 然后,用户按下后退按钮返回所述活动。

However, as soon as the back button has been pressed I need to make a method call. 但是,只要按下后退按钮,我就需要进行方法调用。 Please note that I cannot simply add the method after the following code in the activity, as this will impact the time the user has to interact with the AlertDialog instance. 请注意,我不能简单地在活动中添加以下代码后的方法,因为这会影响用户与AlertDialog实例交互的时间。

The method call needs to happen as soon as the back button has been pressed form the WIFI settings menu . 一旦从WIFI设置菜单按下后退按钮,就需要进行方法调用 Please inform me of how I can implement this. 请告诉我如何实现这一点。

Here is the code: 这是代码:

alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
          Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
          startActivity(intent);
         }
     });

class member 班级成员

private static final int WIFI_REQUEST = 1234;

Use startActivityForResult 使用startActivityForResult

alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
      startActivityForResult(intent, WIFI_REQUEST);
     }
 });  

In the activity class 在活动类中

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

    switch (requestCode)
    {
         case WIFI_REQUEST:
              // Call your method here
              break;
    }
}

You can Override the onResume() method of the calling Activity . 您可以Override调用ActivityonResume()方法。 As soon soon as the user presses the "back" button the onResume() method is sure to get called so you should be able to put your method call here 一旦用户按下“后退” buttononResume()方法肯定会被调用,所以你应该可以在这里调用你的方法

private boolean inwifisettings;

public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
    inwifisettings = true;
    startActivity(intent);
}

@Override public void onWindowFocusChanged(boolean hasFocus)
{
    if(inwifisettings & hasFocus)
    {
         doSomething();
         inwifisettings = false;
    }
}

You should not use onResume() or startActivityForResult()/onActivityResult() for this purpose. 为此,您不应该使用onResume()或startActivityForResult()/ onActivityResult()。 Quoting the Android documentation: http://developer.android.com/reference/android/app/Activity.html 引用Android文档: http//developer.android.com/reference/android/app/Activity.html

public void startActivityForResult (Intent intent, int requestCode, Bundle options) public void startActivityForResult(Intent intent,int requestCode,Bundle options)
Note that this method should only be used with Intent protocols that are defined to return a result. 请注意,此方法只应与定义为返回结果的Intent协议一起使用。 In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. 在其他协议(例如ACTION_MAIN或ACTION_VIEW)中,您可能无法获得预期的结果。 For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result. 例如,如果要启动的活动使用singleTask启动模式,则它将不会在您的任务中运行,因此您将立即收到取消结果。

public void onWindowFocusChanged (boolean hasFocus) public void onWindowFocusChanged(boolean hasFocus)
This is the best indicator of whether this activity is visible to the user. 这是该活动是否对用户可见的最佳指标。
the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity. 系统可以显示系统级窗口(例如状态栏通知面板或系统警报),它将暂时占用窗口输入焦点而不暂停前台活动。

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

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