简体   繁体   English

如何知道Activity在Android中何时完成?

[英]How to know when Activity is finished in android?

In my android app I want to change the input method. 在我的Android应用中,我想更改输入法。 So I start a new Activity which shows the language settings in the device. 因此,我开始一个新的Activity ,该Activity显示设备中的语言设置。 Then user can change it. 然后用户可以更改它。 However then I want to know that if the user has changed it. 但是然后我想知道用户是否更改了它。 So I wrote a function for that also. 所以我也为此写了一个函数。 My code so far is... 到目前为止,我的代码是...

Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
                    startActivityForResult(enableIME,0);


                    if(isInputMethodEnabled()){
                        activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
                        activateshadow.setText("Deactivate Shadow");
                        prefs.edit().putBoolean("Activate", false).commit();
                    }else{
                        Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
                    }

my is inputMethodEnabled function is.... 我的是inputMethodEnabled函数是...。

  public boolean isInputMethodEnabled() {
  boolean isIME ;  
  String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    String [] name = id.split("/.");
   // Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
    if(name[1].contains("SimpleIME") ){
        isIME = true ;
    }else{
        isIME = false;
    }
   //  Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
    return isIME;
}

if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . if(isInputMethodEnabled())总是失败,因为当new intent(settings)打开时,将输入方法更改为simpleIME需要花费一些时间。 How to fix this problem? 如何解决这个问题?

You catch when a launched Activity returns in onActivityResult . 当启动的Activity返回onActivityResult时,您会捕获到。 The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity 's result. 您提供给startActivityForResultrequestCode以及Activity的结果将是一个参数。 The Activity may also set other data which you didn't ask about. Activity可能还会设置您没有询问的其他数据。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
        if (resultCode == /*Result1*/)
            //Do something
        else {
            //Do something else
        }
    }
}

You need a unique id when calling startActivityForResult(enableIME,0); 调用startActivityForResult(enableIME,0);时需要一个唯一的ID startActivityForResult(enableIME,0);

startActivityForResult(enableIME, 555);

Better still replace 555 with a named variable. 最好还是用命名变量替换555。

if u look at android life cycle, when activity is finished whole android call onDestroy() method. 如果您查看android的生命周期,则当活动结束时,整个android会调用onDestroy()方法。 so u can call and override this method. 这样您就可以调用并覆盖此方法。 just need write: 只需要写:

@override
protected void onDestroy(){
// code
super.onDestroy();
}

u can manage and override all of life cycle's parts in android eg: onResume to get current activity 您可以在android中管理和覆盖生命周期的所有部分,例如:onResume以获取当前活动

i hope this help u 我希望这对你有帮助

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

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