[英]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.