2016-10-14 8 views
0

startActivityForResultを使用して別のアクティビティから結果を取得する方法を知っていますが、問題は3つのアクティビティA、B、Cがあります。すべての活動に戻る必要があります。現在のアクティビティから開始されていないアクティビティから特別な値を取得

アクティビティAからアクティビティBを開き、アクティビティBからアクティビティCを開いたときアクティビティCで戻るボタンを押したときに、結果をアクティビティAに戻すにはどうすればよいですか?

活動A:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == 1) { 
     if(resultCode == Activity.RESULT_OK){ 
      int result=data.getIntExtra("result", 0); 
      SetNotification(result); 
     } 
    } 
} 

活動C:

@Override 
public void onBackPressed() { 
    Intent returnIntent = new Intent(C.this, A.class); 
    returnIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    returnIntent.putExtra("result", unreadCount); 
    startActivity(intent); 
} 

を次にあなたがAさんのonCreate()であなたのエクストラを取得することができます

//This works for activity B which is started directly from activity A 
@Override 
public void onBackPressed() { 
    Intent returnIntent = new Intent(); 
    returnIntent.putExtra("result", unreadCount); 
    setResult(Activity.RESULT_OK,returnIntent); 
    finish(); 
} 
+0

を保護次のようにActivityBを行うことができます – clavio

答えて

1

「アクティビティCで戻るボタンを押すと、結果をアクティビティAに返すことができますか?」

戻ってこないでください。前方に移動し、若いバッタ:P

Intent intent = new Intent(this, activityClass); 

// FLAG_ACTIVITY_NEW_TASK : If set, this activity will become the start of a new task on this history stack. 
// FLAG_ACTIVITY_CLEAR_TOP: If set, and the activity being launched is already running in the current task, 
// then instead of launching a new instance of that activity, all of the other activities on 
// top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

intent.putExtra("result", unreadCount); 

startActivity(intent); 
0

私はそれが好きだろう。

希望します。

0

このような何かたぶん、あなたはアクティビティAにコールをリダイレクトするために活動Bから)(onActivityResult使用することができますActivityBはActivityAにこの結果を渡すことができ

// Activity B 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == 1) { 
      if(resultCode == Activity.RESULT_OK){ 
       // Get data from activity C 
       int result = data.getIntExtra("result", 0); 

       // Create intent with data from activity C 
       Intent returnIntent = new Intent(); 
       returnIntent.putExtra("result", result); 

       // Set the response 
       setResult(Activity.RESULT_OK,returnIntent); 
       finish(); 
      } 
     } 
    } 
0

ActivityBのonActivityResultが呼び出されたときに呼び出されます。

を@Override は個人的に私は、静的変数に格納します無効onActivityResult(int型requestCode、int型のresultCode、テントデータ){

if (requestCode == 1) { 
    if(resultCode == Activity.RESULT_OK){ 
     int result=data.getIntExtra("result", 0); 

     Intent returnIntent = new Intent(); 
     returnIntent.putExtra("result", result); 
     setResult(Activity.RESULT_OK,returnIntent); 
     finish(); 
    } 
} 

}

関連する問題