onNewIntent不叫上重启 [英] onNewIntent not called on restart

查看:359
本文介绍了onNewIntent不叫上重启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闹钟应用程序,即使用报警管理器和广播接收器。
应用程序是一个单一的活动,4个片段。当警报响起的方法的onReceive发送意图的主要活动,主要活动收到此意图的方法onNewIntent,然后移动到正确的片段。一切工作正常,当闹钟响起的应用程序关闭后除。

在我破坏应用程序,报警仍然熄灭,从广播接收火灾的意图,但onNewIntent方法确实赶上意图和移动应用正确的片段。

下面是在移动到主要活动的广播接收机类意图

 意图alarmIntent =新意图(背景下,ClockActivity.class);
                alarmIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
                alarmIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                alarmIntent.putExtra(报警名,receivedAlarm.getmName());
                context.startActivity(alarmIntent);

下面是我的主要活动我onNewIntent方法时,当应用程序被关闭就是所谓的报警是没有得到调用。

  @覆盖
保护无效onNewIntent(意向意图){
    super.onNewIntent(意向);    PhraseFragment phraseFragment =新PhraseFragment();    字符串activeName = intent.getStringExtra(报警名称);    捆绑ARGS =新包();
    args.putString(activeName,activeName);
    phraseFragment.setArguments(参数);    getFragmentManager()调用BeginTransaction()
            .replace(R.id.container,phraseFragment)
            .addToBackStack(语)
            。承诺();}


解决方案

它有点迟,但也许这可以帮助别人。

当我看到活动被打开的背景时onNewIntent被调用。当您发送的意图,未在后台运行的活动,你可以通过getIntent在onResume()获取它。

我想code改变你下面的内容。

  @覆盖
保护无效onResume(){
    super.onResume();    意向意图= getIntent();
    字符串activeName = intent.getStringExtra(报警名称);    如果(activeName!= NULL){
        PhraseFragment phraseFragment =新PhraseFragment();        捆绑ARGS =新包();
        args.putString(activeName,activeName);
        phraseFragment.setArguments(参数);        getFragmentManager()调用BeginTransaction()
                .replace(R.id.container,phraseFragment)
               .addToBackStack(语)
               。承诺();
     }
}@覆盖
保护无效onNewIntent(意向意图){
    super.onNewIntent(意向);
    setIntent(意向);
}

在这种情况下,你需要检查,如果你收到onResume()的意图包含所需要的数据。

请注意,我还没有找到文档中此任何引用。它只是结束时我得到通过试验。

I have an alarm clock app, that is using Alarm Manager and a Broadcast Receiver. The app is one single activity and 4 fragments. When an alarm goes off the onReceive method sends an intent to the main activity, the main activity receives this intent in the method onNewIntent and then moves to the correct fragment. Everything works fine, except when an alarm goes off after the app has been closed.

Once I destroy the app, the alarm still goes off and the intent from the broadcast receiver fires, but the onNewIntent method does catch the intent and move the app to the correct fragment.

Here is the Intent that is in the broadcast receiver class that moves to the main activity

Intent alarmIntent = new Intent( context, ClockActivity.class );
                alarmIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
                alarmIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                alarmIntent.putExtra("Alarm Name", receivedAlarm.getmName());
                context.startActivity(alarmIntent);

Here is my onNewIntent method in my main activity that is not getting called when the alarm is called when the app is closed.

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    PhraseFragment phraseFragment = new PhraseFragment();

    String activeName = intent.getStringExtra("Alarm Name");

    Bundle args = new Bundle();
    args.putString("activeName", activeName);
    phraseFragment.setArguments(args);

    getFragmentManager().beginTransaction()
            .replace(R.id.container, phraseFragment)
            .addToBackStack("phrase")
            .commit();

}

解决方案

Its a bit late but maybe this can help someone.

As I see onNewIntent is called when the activity is opened on the background. When you send an intent to an activity that isn't running on the background you can retrieve it through getIntent on onResume().

I would change you code to the following.

@Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    String activeName = intent.getStringExtra("Alarm Name");

    if (activeName != null){
        PhraseFragment phraseFragment = new PhraseFragment();

        Bundle args = new Bundle();
        args.putString("activeName", activeName);
        phraseFragment.setArguments(args);

        getFragmentManager().beginTransaction()
                .replace(R.id.container, phraseFragment)
               .addToBackStack("phrase")
               .commit();
     }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

In this case you need to check if the intent you've receive in onResume() contains the data you require.

Note that I haven't found any reference to this in the documentation. Its just the conclusion i got by experimenting.

这篇关于onNewIntent不叫上重启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆