2015-02-11 138 views
0

我在我的應用程序中使用了最新的棒棒糖樣式導航抽屜。有關更多信息,請參閱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); 

回答

1

您可以添加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 
     } 
    } 
}