从某些Fragment
我这样称呼Activity
:
private void setEmailChangeListener() {
emailChangeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), EmailChangeActivity.class);
startActivityForResult(intent, 1);
}
});
}
在Activity
中,我对数据库中的电子邮件进行了一些更改,然后更新本地数据库中的信息(更新的电子邮件和更新时间和日期)。然后我拨打setResult
:
if (!error) {
JSONObject user = jObj.getJSONObject("user");
String updatedAt = user.getString("updated_at");
String userUniqueId = db.getUserUniqueId();
db.updateUpdatedAt(updatedAt, userUniqueId);
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
showSnackbarInfo(errorMsg, R.color.snackbar_error);
}
在已打开Activity
的{{1}}中,我想要Fragment
Fragment
和detach
更新attach
中的信息,以便对其进行更新。我在onActivityResult
这样做:
// Result From Other Activities
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
Log.i("result", "reqCode = 1, zmieniam adres email w nav draw i odswierzam fragment");
navHeaderEmail.setText(db.getEmailAdress());
Fragment fragment = getSupportFragmentManager().findFragmentByTag(
TAG_FRAGMENT_ACCOUNT_SETTINGS);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.detach(fragment);
fragmentTransaction.attach(fragment);
fragmentTransaction.commit();
showSnackbarInfo(getString(R.string.inf_email_change_success),
R.color.snackbar_success);
}
}
在致电setResult
后,没有任何事情发生,就像我没有正确设置结果或其他东西。你能帮我解决这个问题吗?
这是架构应该如何工作:
修改
这是我的AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nazwamarki.myapplication">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/application_name"
android:theme="@style/AppTheme">
<!-- Home Activity -->
<activity
android:name=".HomeActivity"
android:label="@string/application_name"
android:theme="@style/AppTheme.Normal">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name=".LoginActivity"
android:label="@string/login_activity_name"
android:parentActivityName=".HomeActivity"
android:theme="@style/AppTheme.Logreg">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
<!-- Sign Up Activity -->
<activity
android:name=".SignUpActivity"
android:label="@string/register_activity_name"
android:parentActivityName=".HomeActivity"
android:theme="@style/AppTheme.Logreg">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
<!-- Email Change Activity -->
<activity
android:name=".EmailChangeActivity"
android:label="@string/change_email_activity_name"
android:theme="@style/AppTheme.Normal">
</activity>
<!-- Password Change Activity -->
<activity
android:name=".PasswordChangeActivity"
android:label="@string/change_password_activity_name"
android:theme="@style/AppTheme.Normal">
</activity>
<!-- Recipe Activity -->
<activity
android:name=".RecipeActivity"
android:label="@string/recipe_activity_name"
android:parentActivityName=".HomeActivity"
android:theme="@style/AppTheme.Normal">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
</application>
</manifest>
答案 0 :(得分:0)
getActivity().startActivityForResult(intent, 1);
或覆盖它自己的片段中的onActivityResult
我有反过来的问题我试图在我的片段中使用onActivityResult,但有时它会转到父活动,所以我最终使用第一行并在父行为中处理它