android - 在 Activity 之间传递数据 - Android SDK

标签 android android-intent android-activity

我已经阅读了 2 天之前发布的问题和答案,并且尝试了建议的每个变体,并在我的 list 中将我的 launchMode 属性设置为“标准”。

我试图在按下按钮后将数据从我的第二个 Activity 传回我的第一个 Activity 。按下按钮后,第一个 Activity 启动,但它不会返回到我的 onActivityResult() 方法。我不明白为什么会这样。

这是我在 Activity 2 中的代码:

   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();    

        }
    });

这是 Activity 1:

 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()

我不确定我的 Activity 回调是否未正确放置在代码中,或者我是否没有正确触发 Intent ,或者我是否没有使用正确的方法设置回调或其他什么。我知道已经讨论过这个话题,但我没有想法。谢谢。

最佳答案

你刚刚弄反了。如果 Activity1 应该启动 Activity2 并且 Activity2 应该将结果发送回 Activity1,您需要这样做:

在 Activity 1 中:

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

在 Activity 2 中:

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()

关于android - 在 Activity 之间传递数据 - Android SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938775/

相关文章:

android - 构建android源代码树时出现错误python2.7或chromium

android - 如何在 android-studio 0.3.6 中运行 Gradle 1.9?

android - 如何使数组适配器中的 Intent.ACTION_CALL android 面向 Activity 未找到异常

android-layout - 如何在单个应用程序中使用应用程序小部件创建 Android 应用程序

java - ListView 项目文本不改变颜色

Android GCM 已成功发送但在某些设备上未收到

java - onCreate 在 finish() 之后没有被调用?

Android:网络 radio 在 ACTION_SHUTDOWN 事件之前关闭。事件顺序在 ICS 中更改

android - onTouchEvent() 回调不起作用

android - 关闭android应用程序中所有正在运行的 Activity ?