在Android导航抽屉中手动切换导航选项卡
问题描述:
我在我的应用程序中使用了最新的棒棒糖样式导航抽屉。有关更多信息,请参阅this example。我使用Fragments显示不同的导航选项卡。现在,我需要打开,当我从Android设备的通知栏中单击某个通知时,让我们说出抽屉中的第5项。我被困在如何通过点击通知直接切换到该片段。我非常清楚如何使用Activity来完成这项工作。任何人都可以请建议我任何解决方案?在Android导航抽屉中手动切换导航选项卡
在此先感谢。
解决:
我已经按照ZIEM的回答解决了这个问题。我刚才添加以下行来打开它作为一个新的屏幕,并清除旧的活动堆栈:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
答
您可以添加PendingIntent
到通知的click:
PendingIntent resultPendingIntent;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
...
.setContentIntent(resultPendingIntent);
接下来,你需要处理通知的Intent
你的内活动。
实施例:
// How to create notification with Intent:
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("open", 1);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(resultPendingIntent);
int mNotificationId = 33;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
//How to handle notification's Intent:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent() != null && getIntent().hasExtra("open")) {
int fragmentIndexToOpen = getIntent().getIntExtra("open", -1)
// show your fragment
}
}
}