繁体   English   中英

打开通知时如何更新活动

[英]How to Update activity when open notification

我正在处理通知,但遇到了问题。 当我点击通知时,我有一个活动已经打开,我不想再次打开它,只需更新当前活动。

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
 Intent pushNotification = null;
                if (NotificationType != 0 && NotificationType != 2 && NotificationType != 5 && NotificationType != 26) {
                    pushNotification = new Intent(getApplication(), SplashScreen.class);
                    pushNotification.putExtra("NotificationType", NotificationType);
                    pushNotification.putExtra("ReferenceID", ReferenceID);
                    pushNotification.putExtra("NotificationID", ReferenceID);
                    pushNotification.putExtra("isread", ReferenceID);
                    showNotificationMessage(getApplicationContext(), title, message, time, pushNotification);
                } else if (NotificationType == 0 || NotificationType == 2 || NotificationType == 5 || NotificationType == 26) {
                    showNotificationMessageWithNoAction(getApplicationContext(), title, message, title, null);
                }
}

谁能告诉我单击通知时如何更新活动?


你只需要声明launchModesingleTask使确保多个相同的屏幕没有打开。

活动有四种启动模式。 他们是:
1. 标准
2. 单顶
3. 单任务
4. 单实例

请参考这个链接点击这里

<activity android:name="YOUR_SPLASH_ACTIVITY"
            android:launchMode="singleTask"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Java代码中,您只需覆盖onNewIntent方法,即可刷新activity

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

    /**
     * HERE YOU JUST REFRESH , YOUR ACTIVITY 
     */

}

我不知道这是否是你需要的,但你可以这样做

  finish();
  startActivity(getIntent());

让我知道你真正需要什么。

编辑:

如果你需要保持你的活动状态完整,你可以这样做

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("MyString", "Welcome back to Android");
 }

这将保存您的活动状态

然后你像这样检索 UI 状态

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String myString = savedInstanceState.getString("MyString");
  }

最好的方法是使用您的 Activity 的onNewIntent(Intent)方法。

您可以使用此方法的Intent参数来获取您的Intent Extras ,因为getIntent()将首先提供启动 Activity 的 Intent。

public class MainActivity extends Activity {
    public void onCreate(Bundle SavedInstanceState) {
    //Initial loading
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent.getStringExtra("methodName").equals("myMethod")) {
            //Update the existing screen
        }
    }
}

暂无
暂无

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

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