2012-06-07 4 views
1

이전에 게시 된 질문과 답변을 2 일간 읽었으며 모든 변경 제안을 시도했으며 내 매니페스트에서 내 launchMode 속성을 "표준"으로 설정했습니다.액티비티 간 데이터 전달 - Android SDK

버튼을 누른 후 두 번째 활동의 데이터를 첫 번째 활동으로 다시 전달하려고합니다. 버튼을 누르면 첫 번째 활동이 시작되지만 다시 onActivityResult() 메소드로 돌아 가지 않습니다. 왜 이런 일이 일어나는지 알 수 없습니다. 내 활동 콜백이 코드에서 제대로 배치되지 않은 경우

public class Activity1 extends Activity { 
/** Called when the activity is first created. */ 
@SuppressWarnings("deprecation") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    Button addAlbum = (Button) findViewById(R.id.btnMain); 
    addAlbum.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Intent i = new Intent(); 
      i.setClassName("jorge.jorge.jorge", 
        "jorge.jorge.jorge.Activity2"); 
      startActivity(i); 

     } 
    }); 

}// end of onCreate() 

    //******************Callback Method**************************** 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    //Checks if we got to ActivityResult 
    System.out.println("hello test 2: We got to Activity1"); 

    if (resultCode == RESULT_OK) 

    { 

     Bundle returndata = data.getExtras(); 

     String strAlbum = returndata.getString("strAlbum"); 

     String strBand = returndata.getString("strBand"); 

     String strGenre = returndata.getString("strGenre"); 

     // check to see if we got the variable values from activity2 
     System.out.println("hello test 2: We got to Activity1 with variables - " 
       + strBand + " - " + strAlbum + " - " + strGenre); 

     //Create table layout to contains views with variable values 
     TableLayout table = new TableLayout(this); 
     table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     //creates row with parameters 
     TableRow row = new TableRow(this); 
     row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 

     //text views to contain variable values 
     TextView tv1 = new TextView(this); 
     tv1.setText(strBand); 
     row.addView(tv1); 
     TextView tv2 = new TextView(this); 
     tv2.setText(strAlbum); 
     row.addView(tv2); 
     TextView tv3 = new TextView(this); 
     tv3.setText(strGenre); 
     row.addView(tv3); 
     //adds the table row to the table layout 
     table.addView(row); 
    } 

}// end onActivityResult() 

}

는 잘 모르겠어요 :

여기
Button btnAdd = (Button) findViewById(R.id.btnAdd); 
    btnAdd.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      //Check that message is printed out to LogCat 
      System.out.println("hello test1 Activity2"); 

      EditText band = (EditText) findViewById(R.id.txtBand); 
      band.setFilters(new InputFilter[] { 
        new InputFilter.LengthFilter(9) 
      }); 
      EditText album = (EditText) findViewById(R.id.txtAlbum); 
      album.setFilters(new InputFilter[] { 
        new InputFilter.LengthFilter(9) 
      }); 
      final Spinner genre = (Spinner) findViewById(R.id.spin_genre); 
      TextView selection = (TextView)genre.getSelectedView(); 

      CharSequence strBand = band.getText(); 
      CharSequence strAlbum = album.getText(); 
      CharSequence strGenre = selection.getText(); 

      //Check that we got input values 
      System.out.println("hello test Activity2- " + 
        strBand + " - " + strAlbum + " - " + strGenre); 

      //**********Intent Declaration************ 

      Intent i = new Intent(getApplicationContext(), Activity1.class); 

      i.putExtra("strBand", strBand); 
      i.putExtra("strAlbum", strAlbum); 
      i.putExtra("strGenre", strGenre); 
      startActivityForResult(i, 0); 
      setResult(RESULT_OK, i); 
      finish();  

     } 
    }); 

활동 1의 :

여기에 활동 2에서 내 코드입니다 또는 의도를 올바르게 실행하지 못하거나 올바른 방법으로 콜백을 설정하지 않은 경우. 이 주제는 논의되었지만 아이디어가 없습니다. 감사.

답변

8

방금 ​​거꾸로했습니다. activity1에이 startActivity2로 가정하고 activity2에가 다시 activity1에 그 결과를 보낼 것으로 예상되는 경우에, 당신은 이런 식으로 작업을 수행해야합니다 activity1에의

: activity2에의

Intent i = new Intent(); 
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2"); 
startActivityForResult(i); // This starts Activity2 and waits for the result 

:

Intent i = new Intent(getApplicationContext(), Activity1.class); 
i.putExtra("strBand", strBand); 
i.putExtra("strAlbum", strAlbum); 
i.putExtra("strGenre", strGenre); 
setResult(RESULT_OK, i); 
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult() 
+0

아, 그 말이 맞습니다. 그것은 내 자신에 대해 이것을 지적하는 적절한 예를 찾을 수있는 일종의 미친 짓이다. 나는 좋은 대답으로 텍스트 북과 여러 게시물을 참고했지만이 맥락에서는 부족하다. 고마워, 고마워. – rocklandcitizen

+1

도움이 될 수있어서 기쁩니다. 내 답을 수락하면 나를 도울 수 있습니다 (녹색 체크 표시를 클릭하십시오). –