在这种情况下,两个活动A和B应该以A为结果调用B的方式进行交互。
但A从来没有从B获得任何数据。
此代码看起来像我见过的任何教程或SO问题。
调试会话确认调用了setResult。
这个问题让我失去了动力,如果有人能指出我所缺少的东西会很好。
编辑:代码在我的物理目标设备上执行时不起作用,但在nexus 5设备上运行查找。我能找到什么来找出原因?
MainActivity.java
package iifym.apps.rnoennig.de.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
Intent in = new Intent(this, SecondActivity.class);
in.putExtra("mau", "bar");
startActivityForResult(in, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// data is null ;(
Toast.makeText(this, String.valueOf(data), Toast.LENGTH_SHORT).show();
}
}
SecondActivity.java
package iifym.apps.rnoennig.de.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Toast.makeText(this, String.valueOf(getIntent()), Toast.LENGTH_SHORT).show();
}
public void onClick(View view) {
setResult(RESULT_OK, new Intent().putExtra("foo", "bar"));
finish();
}
}
布局/ main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
布局/ second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iifym.apps.rnoennig.de.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
答案 0 :(得分:1)
适合我。 (相同的布局和清单)。
主要
public class MainActivity extends AppCompatActivity {
public static final int MAIN_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Intent in = new Intent(MainActivity.this, SecondActivity.class);
in.putExtra("mau", "start 2nd");
startActivityForResult(in, MAIN_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MAIN_REQUEST) {
if (resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, data.getStringExtra("foo"), Toast.LENGTH_SHORT).show();
}
}
}
}
第二
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent sent = getIntent();
if (sent != null) {
Toast.makeText(SecondActivity.this, sent.getStringExtra("mau"), Toast.LENGTH_SHORT).show();
}
}
public void onClick(View v) {
setResult(RESULT_OK, new Intent().putExtra("foo", "finish 2nd"));
finish();
}
}