如何导航到从Notification而不是覆盖OnBackPressed()的活动中启动的另一个活动?

时间:2014-11-18 12:20:46

标签: android android-activity notifications android-manifest

我有3个活动,假设MainActivity,FirstActivity,NotificationActivity,当用户点击我的应用活动C的通知时,我想要的是用户能够在按下后退按钮时回到B,我想要为了实现这一点而不重写OnBackPressed()方法做一些R& D我发现了一种方法,使用清单来指定父活动回到另一个活动,它的工作是从B-> A和C-> A但我不知道如何让C-> B工作。我将发布我正在处理的清单文件。如果需要,我会在需要时发布课程文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationmultipledemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_first"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
    <activity android:name=".NotificationActivity" 
        android:label="@string/title_activity_notification" 
        android:parentActivityName=".FirstActivity">
        <meta-data android:name="android.support.PARENT_ACTIVITY" 
            android:value=".FirstActivity" />
    </activity>


</application>

这是A类,即MainActivity

public class MainActivity extends Activity
{
   private NotificationManager myNotificationManager;
   private int notificationIdOne = 111;
   private int notificationIdTwo = 112;
   private int numMessagesOne = 0;
   private int numMessagesTwo = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button notOneBtn = (Button) findViewById(R.id.notificationOne);
      notOneBtn.setOnClickListener(new View.OnClickListener() {
         @Override
        public void onClick(View view) {
            displayNotificationOne();
         }
      });

      Button notTwoBtn = (Button) findViewById(R.id.notificationTwo);
      notTwoBtn.setOnClickListener(new View.OnClickListener() {
         @Override
        public void onClick(View view) {
            displayNotificationTwo();
         }
      });

   }

   protected void displayNotificationOne() {

      // Invoking the default notification service
      NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);  

      mBuilder.setContentTitle("New Message with explicit intent");
      mBuilder.setContentText("New message from Cheers received");
      mBuilder.setTicker("Explicit: New Message Received!");
      mBuilder.setSmallIcon(R.drawable.ic_launcher);

      // Increase notification number every time a new notification arrives 
      mBuilder.setNumber(++numMessagesOne);

      // Creates an explicit intent for an Activity in your app 
      Intent resultIntent = new Intent(this, NotificationActivity.class);
      //resultIntent.putExtra("notificationId", notificationIdOne);
   // when the user presses the notification, it is auto-removed
      mBuilder.setAutoCancel(true);
      //This ensures that navigating backward from the Activity leads out of the app to Home page
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
      // Adds the back stack for the Intent
      stackBuilder.addParentStack(FirstActivity.class);

      // Adds the Intent that starts the Activity to the top of the stack
      stackBuilder.addNextIntent(resultIntent);
      PendingIntent resultPendingIntent =
         stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT 
         );
      // start the activity when the user clicks the notification text
      mBuilder.setContentIntent(resultPendingIntent);

      myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      Date now = new Date();
        long uniqueId = now.getTime();//use date to generate an unique id to differentiate the notifications.
      // pass the Notification object to the system
        Calendar c = Calendar.getInstance();
        SimpleDateFormat simpledateformat_notification = new SimpleDateFormat("HH:mm:ss");
        String Notificationdate = simpledateformat_notification.format(c.getTime());
        String[] Notification_time = Notificationdate.split(":");
        //int Beacon_hour = Integer.parseInt(Notification_time[0]); //7
        int Notification_minutes =Integer.parseInt(Notification_time[2]);

      myNotificationManager.notify(Notification_minutes, mBuilder.build());
   }  
}

1 个答案:

答案 0 :(得分:0)

在displayNotificationOne()内部替换

stackBuilder.addParentStack(FirstActivity.class);

stackBuilder.addParentStack(NotificationActivity.class);

如果您希望用户在按下后退按钮时从NotificationActivity导航到FirstActivity,那么您也可以在此处从FirstActivity转到MainActivity,因为清单。如果您不希望发生这种情况,请删除FirstActivity活动部分中的元数据和parentActivityName。