简体   繁体   English

如何仅在活动进入屏幕时调用“ onResume”,而不是通过其他方法调用它

[英]How to make `onResume` only be called when the activity comes to screen and not by other methods calling it

I have a situation with onResume() method and I don't know how to solve it. 我遇到了onResume()方法的情况,但不知道如何解决。 conssider the following code: 考虑以下代码:

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        Log.d("tag", "onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //do something only when everytime the activity comes to screen
        Log.d("tag", "onResume");
    }

    private void myMethod() {
        this.onResume();
    }
}

If we asume that myMethod will definitly be called, I don't want to let onResume() to execute //do something only when everytime the activity comes to screen . 如果我们假设将明确调用myMethod ,那么我不想让onResume() //do something only when everytime the activity comes to screen执行//do something only when everytime the activity comes to screen I should note that myMethod is a fixed method and cannot be changed. 我应该注意, myMethod是固定方法,不能更改。

PS:The reason that I am asking this question is that I have a simillar situation with PermissionDispatcher library with android 6 and I want to call a "risky permission" in the onReume() method but if the user denies the permission, it will call the onReume() again, and since the permission required task is in the onResume() , the permission will be denied again and cauases an inifite loop PS:我问这个问题的原因是,我在android 6的PermissionDispatcher库中遇到了类似情况,我想在onReume()方法中调用“风险权限”,但是如果用户拒绝该权限,它将调用再次执行onReume() ,并且由于所需权限的任务位于onResume() ,因此该权限将再次被拒绝并阻止inifite循环

could anyone give me a suggestion? 谁能给我一个建议?

UPDATE: here is the permissionDispatcher library and the issue that is related to my problem 更新:这里是permissionDispatcher库和问题是关系到我的问题

verify this method Already Executed or not simply in if Condition 验证此方法是否已经执行或是否仅在if条件中执行

Bool a=true;

     @Override
            protected void onResume() {
                super.onResume();
                //do something only when everytime the activity comes to screen
                if(a==true)
                   {
                   //your Actions 
                    }
        else if(a==false)
        {
        //do nothing 
        }
            }

Use SharedPreferences in onResume 在onResume中使用SharedPreferences

@Override
protected void onResume() {
    super.onResume();
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean firstStart = settings.getBoolean("firstStart", true);
   if(firstStart) {
    //do something only when everytime the activity comes to screen
    Log.d("tag", "onResume");
    //display your Message here
     SharedPreferences.Editor editor = settings.edit();
     editor.putBoolean("firstStart", false);
     editor.commit();
   }

}

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

相关问题 返回活动时未调用Android onResume() - Android onResume() not being called when returning to an Activity 仅当恢复活动时才onResume() - onResume() only if Resume Activity 从片段返回时,不会调用主页活动的 OnResume() - OnResume() is not called of the home activity when back pressed from a fragment 仅在第二次访问活动时才发生OnResume - OnResume to only happen when activity is accessed the second time 我的活动的onPause / OnResume方法每分钟都会被无故调用一次 - My activity's onPause/OnResume methods are called once every minute for no reason 仅在应用程序启动时显示启动画面,而不是每次调用包含启动画面代码的活动时显示启动画面 - Display splash screen only when the app is launched and not every time when the activity containing splash screen code, is called 如何处理仅由同一类的其他方法调用但不应由实例调用的方法? - How to treat methods which are only called by other methods from the same class but shouldn't be called by an instance? onResume从其他活动返回时的行为不符合预期 - onResume not behaving as expected on return from other activity 选中复选框后如何进行全屏活动 - How to make full screen activity when a box is checked 如何进行半屏活动? - How to Make Half Screen Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM