我在阅读多个网页后尝试了很多方法,但是,我无法在两个活动和一个片段之间进行对象的往返。
概述:该对象是描述膳食的Serializable
对象(您吃的东西)。我有一个MainActivity
使用片段,ListView
列出了Meal对象。当我点击ListView
中的一餐时,使用 startActivityForResult 方法启动MealEditorActivity
。这很好用。我可以在此活动中编辑Meal对象。
问题:当我在MealEditorActivity
菜单栏中单击后,我想要重新发送已编辑的Meal对象,但片段中的 onActivityResult 和MainActivity
中都没有被称为。
我做错了什么?
代码段:
...
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add a button below the listview to create new Meal entries
Button newButton = (Button) findViewById(R.id.new_meal_button);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
...
}
});
}
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(LOG_TAG, "onActivityResult in MainActivity triggered");
super.onActivityResult(requestCode, resultCode, data);
}
}
这就是单击一份用餐时片段中MealEditorActivity
的启动方式..
...
public class MainFragmentMealList extends Fragment {
private static final String LOG_TAG = MainFragmentMealList.class.getSimpleName();
protected List<Meal> mealList;
protected final int REQUEST_CODE = 1;
public MainFragmentMealList() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mealList = new ArrayList<Meal>();
// if no data exists in database, the generate some test data
if(mealList.size() == 0) {
Meal meal = new Meal();
meal.setId(mealList.size()+1);
meal.test();
mealList.add(meal);
meal = new Meal();
meal.setId(mealList.size()+1);
meal.test2();
mealList.add(meal);
}
ArrayAdapter<Meal> mealListeAdapter =
new ArrayAdapter<>(
getActivity(), // Die aktuelle Umgebung (diese Activity)
R.layout.meal_list_item, // ID der XML-Layout Datei
R.id.meal_list_item_textview, // ID des TextViews
mealList); // Beispieldaten in einer ArrayList
View rootView = inflater.inflate(R.layout.fragment_meallist_main, container, false);
ListView mealListView = (ListView) rootView.findViewById(R.id.listview_mealliste);
mealListView.setAdapter(mealListeAdapter);
mealListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Meal selMeal = (Meal) adapterView.getItemAtPosition(position);
Intent intent = new Intent(getActivity(), MealEditorActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(MealEditorActivity.EDIT_MEAL_TAG, selMeal);
intent.putExtras(bundle);
startActivityForResult(intent, REQUEST_CODE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(LOG_TAG, "onActivityResult triggered: " + requestCode);
if (resultCode == Activity.RESULT_OK) {
Meal meal = (Meal) data.getExtras().getSerializable(MealEditorActivity.REPLY_MEAL_TAG);
Log.d(LOG_TAG, "Date@onActivityResult : " + meal.getDate());
}
super.onActivityResult(requestCode, resultCode, data);
}
...
MealEditorActivity,应该从finish方法发送结果..
...
public class MealEditorActivity extends AppCompatActivity {
private static final String LOG_TAG = MealEditorActivity.class.getSimpleName();
public static final String EDIT_MEAL_TAG = "EDIT_MEAL";
public static final String REPLY_MEAL_TAG = "REPLY_MEAL";
protected Meal meal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor_meal);
// get the meal object from the MainActivity to be edited
Intent intent = getIntent();
meal = (Meal) intent.getSerializableExtra(EDIT_MEAL_TAG);
// initialize widgets in the view with data from meal object
// and create listeners to update the meal object with user edits
...
}
@Override
public void finish() {
Log.d(LOG_TAG, "Date@finish : " + meal.getDate());
Intent resultIntent = new Intent();
resultIntent.putExtra(MealEditorActivity.REPLY_MEAL_TAG, meal);
setResult(RESULT_OK, resultIntent);
super.finish();
}
@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
// This one is triggered if the back button is clicked in the menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
finish();
return super.onOptionsItemSelected(item);
}
}
这是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.levin.symptomdiary">
<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"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MealEditorActivity"
android:label="@string/action_editor_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.levin.symptomdiary.MainActivity" />
</activity>
</application>
</manifest>
我使用新安装的Android Studio
与移动Emulator
,Android 4.4
目标。
这是我的第一个Android开发,因此,任何帮助都非常感谢。非常感谢你提前!!