我的应用程序中目前有一个选项卡屏幕。我使用通知来通知用户我的应用程序中的状态更改,在正常情况下,当用户单击通知时,它将返回到此选项卡屏幕。
这是我为此使用的代码:
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) context.getSystemService(ns);
Intent notificationIntentRegState = new Intent();
ComponentName cn = new ComponentName(context, TabScreenActivity.class);
notificationIntentRegState.setComponent(cn);
contentIntentRegState = PendingIntent.getActivity(context, 0, notificationIntentRegState, 0);
notificationRegStateText = context.getString(R.string.app_name);
notificationRegState = new Notification(icon, notificationRegStateText, 0);
notificationRegState.setLatestEventInfo(context, notificationRegStateText, context.getString(R.string.notification_text, contentIntentRegState);
mNotificationManager.notify(3, notificationRegState);
但是在我的应用程序中,用户可以从 TabScreen 启动另一个 Activity ,当此 Activity 处于“Activity ”状态时,我希望通知将用户带到新 Activity ,而不是 tabscreen Activity 。但是当点击通知时,它总是转到标签屏幕。
是否可以确保通知始终将用户引导至最近的 Activity ?
最佳答案
您可以重新创建用户创建的最后一个 Activity ,以响应被点击的通知。
在您的待定 Activity 中,您可以添加额外的数据以记住要显示的 Activity :
Intent notificationIntentRegState = new Intent( "showTabScreen" );
ComponentName cn = new ComponentName(context, TabScreenActivity.class);
notificationIntentRegState.setComponent(cn);
notificationIntentRegState.putExtra( CURRENT_ACTIVITY, getCurrentActivityId() );
其中 getCurrrentActivityID() 返回一个常量,您可以使用该常量来标识上次启动的 Activity 。
并且,在您的 tabScreen 的 onCreateMethod 中,您可以检查您收到的 Intent 和额外的参数以响应点击通知再次启动该 Activity
@Override
public void onCreate( Bundle b )
{
String action = getIntent().getAction();
if( action != null && action.equals( "showTabScreen" ) )
{
int activityToShow = getIntent().getIntExtra( CURRENT_ACTIVITY, -1 );
if( activityToShow != -1 )
{
switch( activityToShow )
{
case ACTIVITY_ID_ONE : startActivity( ActivityOne.class );
}//switch
startActivity();
}//if
}//if
}//met
当然,您的 tabScreen 会在您的 manifest.xml 文件中过滤 Intent 名称“showTabScreen”。
问候, 斯蒂芬
关于Android - 总是从通知返回到当前 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6409358/