2017-01-05 64 views
-1

我有一個活動在這裏我用兩個onActvityResults的Java,Android的:onActivityResult不同的參數在一個活動

一個爲CalendarView

@Override 
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) 

一個在谷歌日曆事件檢索:

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

我如何在一項活動中有這兩個?謝謝!

+0

簡單地檢查onActivityResult方法中requestCode的價值,你是好去。請求代碼可以在使用intent –

+0

調用setResult()的活動時爲這兩個活動設置不同的值。 finish();'不關心Intent參數的名字是什麼 –

回答

0

你沒有。你只有一個,但他們應該有不同的代碼,這就是如果結果來自日曆視圖​​或Google日曆或其他東西的話,你可以如何區分。

3

看來你還沒有正確理解這個概念!這可能會幫助您瞭解onActivityResult

通過使用startActivityForResult(Intent intent, int requestCode)可以開始另一個活動,然後收到結果onActivityResult() method.So onActivityResult()該活動是從那裏開始的另一個活動。

onActivityResult(int requestCode, int resultCode, Intent data)檢查這裏的參數。請求代碼在那裏從您得到結果的地方進行過濾。所以你可以使用他們的requestCodes來識別不同的數據!

public class MainActivity extends Activity { 

     // Use a unique request code for each use case 
     private static final int REQUEST_CODE_EXAMPLE = 0x9988; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      // Create an Intent to start AnotherActivity 
      final Intent intent = new Intent(this, AnotherActivity.class); 

      // Start AnotherActivity with the request code 
      startActivityForResult(intent, REQUEST_CODE_EXAMPLE); 
     } 

     //-------- When a result is returned from another Activity onActivityResult is called.--------- // 
     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 

      // First we need to check if the requestCode matches the one we used. 
      if(requestCode == REQUEST_CODE_EXAMPLE) { 

       // The resultCode is set by the AnotherActivity 
       // By convention RESULT_OK means that what ever 
       // AnotherActivity did was successful 
       if(resultCode == Activity.RESULT_OK) { 
        // Get the result from the returned Intent 
        final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA); 

        // Use the data - in this case, display it in a Toast. 
        Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show(); 
       } else { 
        // AnotherActivity was not successful. No data to retrieve. 
       } 
      } 
     } 
    } 

AnotherActivity < - 這是我們用來發送數據到MainActivity

public class AnotherActivity extends Activity { 

     // Constant used to identify data sent between Activities. 
     public static final String EXTRA_DATA = "EXTRA_DATA"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_another); 

      final View button = findViewById(R.id.button); 
      // When this button is clicked we want to return a result 
      button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        // Create a new Intent as container for the result 
        final Intent data = new Intent(); 

        // Add the required data to be returned to the MainActivity 
        data.putExtra(EXTRA_DATA, "Some interesting data!"); 

        // Set the resultCode to Activity.RESULT_OK to 
        // indicate a success and attach the Intent 
        // which contains our result data 
        setResult(Activity.RESULT_OK, data); 

        // With finish() we close the AnotherActivity to 
        // return to MainActivity 
        finish(); 
       } 
      }); 
     } 

     @Override 
     public void onBackPressed() { 
      // When the user hits the back button set the resultCode 
      // to Activity.RESULT_CANCELED to indicate a failure 
      setResult(Activity.RESULT_CANCELED); 
      super.onBackPressed(); 
     } 
    } 

注意之一:在MainActivity現在檢查你startActivityForResult有您指定的REQUEST_CODE 。假設您想要撥打三種不同的活動來獲得結果..所以有三個startActivityForResult來電有三個不同的REQUEST_CODE's。 REQUEST_CODE只不過是您在活動中指定的唯一密鑰,用於唯一標識您的startActivityForResult調用。

一旦您從這些活動接收到數據,您可以檢查什麼是REQUEST_CODE,那麼你知道啊哈,這個結果來自於這個活動。

這就像你發送郵件給你的愛人與豐富多彩的封面,並要求他們在同一封面答覆。那麼如果你從他們那裏收到一封信,你知道是誰給你寄了一封。 awww;)

0

您可以使用onActivityResult方法的requestCode參數。對於使用不同請求代碼的場景,請調用startActivityForResult方法。

E.g.

onActivityResult
private static int CALENDAR_VIEW_CALL = 1; 
private static int CALENDAR_EVENTS_CALL = 2; 

... 

startActivityForResult(intent1, CALENDAR_VIEW_CALL); 

... 

startActivityForResult(intent1, CALENDAR_EVENTS_CALL); 

現在:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CALENDAR_VIEW_CALL) { 
     //do the callback work for calendar view 
    } 
    else if (requestCode == CALENDAR_EVENTS_CALL) { 
     //do the callback work after retrieving the events 
    } 
} 
0

但是你沒有方法使用不同的參數,你有同樣的方法,用同樣的參數,只有參數的名稱是不同的,而你不能在java中這樣做,因爲語言並不知道哪個方法是哪個。

您應該遵循其他的答案,並檢查requestCode

相關問題