2017-08-16 12 views
0

通知上のユーザーのクリックを開始するときgroundd、私は期待どおりPickUpActivityにジャンプします。問題は、ユーザーがクリックまたは戻るボタンをクリックすると、アプリケーションを終了することです。ユーザーが戻るボタンまたは上のボタンをクリックすると、MainScreenActivityにジャンプします。これは、アプリがforegroud中に通知をクリックしたときにうまく動作します。ですから、私はPickUpActivityのボタンの挙動を上書きしたくありません。 は、私はマニフェストで親を設定するのですが、それはあなたのnotifiaction活性のonBackPressedにこれを追加コントロールbackstack通知をクリックすると、アクティビティ

<activity 
    android:name=".screen.rating.PickUpActivity" 
    android:parentActivityName=".screen.main.MainScreenActivity" 
    android:screenOrientation="portrait"> 
    <meta-data 
     android:name="android.support.PARENT_ACTIVITY" 
     android:value=".screen.main.MainScreenActivity" /> 
</activity> 

答えて

1

動作しないPickUpActivityの親を設定するにはどのような方法がMainScreenActivity であります。

通知アクティビティの開始時にputextra()を実行した後。 "onCreat()"

@Override 
public void onBackPressed() { 
    Bundle extras = getIntent().getExtras(); 

    boolean launchedFromNotif = false; 

    if (extras.containsKey("EXTRA_LAUNCHED_BY_NOTIFICATION")) { 
     launchedFromNotif = extras.getBoolean("EXTRA_LAUNCHED_BY_NOTIFICATION"); 
    } 

    if (launchedFromNotif) { 
     // Launched from notification, handle as special case 
     Intent intent = new Intent(this, MainScreenActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     mActivity.startActivity(intent); 
     finish(); 
    } else { 
     super.onBackPressed(); 
    } 
} 
+0

にコードの行の下に追加した後、それは良いアイデアです。ありがとう –

0
Try to start activity your main Activity on BackPressed of Notification Screen 

Implement the below code in your PickUpActivity Screen. 

@Override 
public void onBackPressed() 
{ 
    super.onBackPressed(); 


     Intent intent = new Intent(PickUpActivity.this, MainScreenActivity.class); 
     startActivity(intent); 
     finish(); 

} 
+0

私はBackPressedを無効にしたくない –

1

あなたは、背景や前景のアプリケーションはそう、あなたのアプリケーションの状態を識別することができる方法の下に使用しているときにアプリの状態を特定する必要がありました。

public static boolean isAppIsInBackground(Context context) { 
     boolean isInBackground = true; 
     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { 
      List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); 
      for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { 
       if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 
        for (String activeProcess : processInfo.pkgList) { 
         if (activeProcess.equals(context.getPackageName())) { 
          isInBackground = false; 
         } 
        } 
       } 
      } 
     } else { 
      List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
      ComponentName componentInfo = taskInfo.get(0).topActivity; 
      if (componentInfo.getPackageName().equals(context.getPackageName())) { 
       isInBackground = false; 
      } 
     } 
     return isInBackground; 
    } 

は、単にあなたの背中のプレスイベント

@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     if(NotificationUtils.isAppIsInBackground(context)){ 
      //open your main screen activity MainScreenActivity.class 
     }else{ 
      // what you want 
     } 
    } 
+0

ユーザーがアップまたはバックボタンを押すことができれば、アプリケーションはフォアグラウンドになっているので、問題は解決しません。しかし、私はまた、私のアプリでこのコードが必要とあなたのコードは完全に動作します。どうもありがとうございます! –

+0

あなたは私にユーザーが戻るボタンを押すと言うのですか? –

+0

ユーザがホームボタンまたは戻るボタンを押したときに1つの機能を実行し、両方の場所に電話をかけます。 –

関連する問題