2016-08-13 5 views
0

こんにちは。私はこれをしたい:アクションバーの戻るボタンを押すと、前のアクティビティの値を送信します。チェック値はそこにとどまります。戻るボタンを押すと値が前に送信されます。アクティビティ

public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       // app icon in action bar clicked; go home 
       //i'm giving error because i mustnt create new activity i must to send previous activity.. 
       LoginActivity yeni=new LoginActivity(); 
       yeni.setPassword(""); 
       this.finish(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 

    } 

私はバックボタンが

...アプリケーションをサインアウトしてloginscreenを行くが、私は同じ値のログイン再び活動をログインし、主な活動を開始していない場合ので、私はloginactivityの値を変更する必要があります押してください

明らかに、私が戻ってボタンを押したときに、私はLoginActivityのメソッドにnullの値を送る必要があります。

public void setPassword(String comingpass) 
    { 
     _passwordText.setText(comingpass); 

    } 

それはそこ_passwordtext=null滞在を知っているし、新しいログインを待つときは...

+1

使用startActivityForResult – comeback4you

答えて

0
// call activity with startActivityForResult 
Intent i = new Intent(this, SecondActivity.class); 
startActivityForResult(i, 1); 


//back press 
switch (item.getItemId()) { 
     case android.R.id.home: 
Intent returnIntent = new Intent(); 
returnIntent.putExtra("flag",1); 
setResult(Activity.RESULT_OK,returnIntent); 
finish(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 


//handle back in main activity 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

if (requestCode == 1) { 
    if(resultCode == Activity.RESULT_OK){ 
     if(data.getIntExtra("flag",0)==1){ 
     yeni.setPassword(""); 
    } 
    } 
    if (resultCode == Activity.RESULT_CANCELED) { 
     //Write your code if there's no result 
    } 
} 
} 
+0

あなたは、良い答えていますが、ここでは2つの引数のデフォルト値data.getIntExtra(「フラグ」を取っdata.getIntExtra(「フラグ」)を編集することができ感謝し、0合格)正しいことができます。 –

+0

はい右..編集済み.. – comeback4you

0
in Activity B: 

    @Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(); 
    intent.putExtra("MESSAGE", strtext + ""); 
    setResult(2, intent); 


      super.onBackPressed(); 
} 

in Activity A: 
     Intent itemintent = new Intent(contextt, ActivityB.class); 
       Bundle b = new Bundle(); 
       b.putInt("mflag", 0); 
       itemintent.putExtra("android.intent.extra.INTENT", b); 
       startActivityForResult(itemintent, 2); 

     @Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    try { 
     super.onActivityResult(requestCode, resultCode, data); 
     String sSuName = data.getStringExtra("MESSAGE"); 
     // txtfavouratecount.setText(sSuName); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
関連する問題