Huy*_*y T 47 java android android-lifecycle android-activity
基本上,这就是我正在做的事情
1)设置AlarmManager以执行BroadcastReceiver(BCR)
Intent intent = new Intent(m_Context, BCR.class);
intent.putExtras(extras);
PendingIntent pendingIntent = PendingIntent.getBroadcast(m_Context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, StartTime, pendingIntent)
Run Code Online (Sandbox Code Playgroud)
2)从BCR启动MyActivity
@Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent(context, MyActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
Run Code Online (Sandbox Code Playgroud)
3)如果MyActivity未打开,请将其打开
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.myactivity);
}
@Overide
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我注意到当MyActivity打开时,它的流程如下:
onCreate/onNewIntent - > onResume - > onPause - > onResume
我不确定为什么它会立即进行onPause.我注意到这只发生在标志打开被筛选时.有谁知道为什么会这样?有什么办法可以防止这种行为?
ain*_*aur 29
为了防止其他人遇到这种情况,我似乎只会在通过XML布局将活动内部的片段膨胀时注意到此行为.我不知道这种行为是否也发生在Fragments的兼容性库版本中(我使用的是android.app.Fragment)
Activity#onResume在调用Fragment#onResume任何添加的片段之前,活动似乎会调用一次,然后Activity#onResume再次调用.
TWL*_*TWL 16
如果你有ES File Explorer那么强制停止它.不知何故,他们会中断你的应用程序的生命周期(评论建议某种覆盖).
我onResume被引发两次的问题是因为onPause在创建活动之后某种程度上被调用了......有些东西正在打断我的应用程序.
这只有在安装后第一次打开或从工作室构建后才会发生.
我从另一篇文章中得到了线索,发现这是因为ES文件资源管理器.为什么onResume()似乎被调用两次?
一旦我强制停止ES文件资源管理器,这种打嗝行为就不再发生......在尝试了许多其他提议的解决方案之后,知道它是令人沮丧的.所以要注意像这样的任何其他中断应用程序.
use*_*924 11
如果您每次尝试请求权限都会导致此类问题,请检查您是否已授予权限
requestPermissions 可以导致它:
onCreate
onStart
onResume
onPause
onResume
Run Code Online (Sandbox Code Playgroud)
我正在研究这个问题一段时间,因为在互联网上没有提到这种奇怪的行为.我没有解决方法如何克服这种黑暗行为,但我确实发现了确实发生的情况.
onPause-onResume-onPause-onResume每当app在安装后第一次启动时,就会发生这种情况.您可以通过对代码进行任何更改并从IDE重新运行(包括重新编译)应用程序来简单地调用此行为.
无论您是否使用AppCompat库.我已经测试了这两种情况和行为继续进行.
注意:在Android Marshmallow上测试过.
我从这个线程中借用了有关片段和活动生命周期的代码,这里是(只需复制,粘贴,在清单中声明活动并运行Forest运行):
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestActivity extends Activity {
private static final String TAG = "ACTIVITY";
public TestActivity() {
super();
Log.d(TAG, this + ": this()");
}
protected void finalize() throws Throwable {
super.finalize();
Log.d(TAG, this + ": finalize()");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, this + ": onCreate()");
TextView tv = new TextView(this);
tv.setText("Hello world");
setContentView(tv);
if (getFragmentManager().findFragmentByTag("test_fragment") == null) {
Log.d(TAG, this + ": Existing fragment not found.");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(new TestFragment(), "test_fragment").commit();
} else {
Log.d(TAG, this + ": Existing fragment found.");
}
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, this + ": onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, this + ": onResume()");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, this + ": onPause()");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, this + ": onStop()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, this + ": onDestroy()");
}
public static class TestFragment extends Fragment {
private static final String TAG = "FRAGMENT";
public TestFragment() {
super();
Log.d(TAG, this + ": this() " + this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, this + ": onCreate()");
}
@Override
public void onAttach(final Context context) {
super.onAttach(context);
Log.d(TAG, this + ": onAttach(" + context + ")");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, this + ": onActivityCreated()");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, this + ": onCreateView()");
return null;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, this + ": onViewCreated()");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, this + ": onDestroyView()");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, this + ": onDetach()");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, this + ": onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, this + ": onResume()");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, this + ": onPause()");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, this + ": onStop()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, this + ": onDestroy()");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定发生了什么,但我怀疑您的活动正在重新启动,因为设置屏幕被系统视为配置更改.您可以尝试在每次调用时记录配置,onResume以查看是否发生了这种情况,如果是,则实际发生了什么变化.然后,您可以修改清单,告诉系统您的活动将自行处理更改.
protected void onResume() [
super.onResume();
Configuration config = new Configuration();
config.setToDefaults();
Log.d("Config", config.toString());
. . .
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28351 次 |
| 最近记录: |