使用onSupportNavigateUp方法从另一个活动返回后,我的MainActivity没有调用onActivityResult。我知道onSupportNavigateUp可以正常工作,因为我已将其记录下来。
但是当MainActivity返回时,我没有得到Log(“ RESULT”)测试,因此甚至没有进入if语句。
在下面查看我的代码
更新:在MainActivity上按返回按钮时,我注意到了一个奇怪的行为。在退出应用程序之前,似乎有多个实例(例如8或10)。
更新:加入了我的清单
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FragmentManager mManager = getSupportFragmentManager();
FirebaseUser mCurrentUser;
FirebaseAuth mAuth;
private TextView mEmailHeader;
private User mUser;
private DatabaseReference mDatabase;
private static final int REQUEST_CODE = 222;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference();
initializeViews();
mUser = new User();
mAuth = FirebaseAuth.getInstance();
if (mAuth != null) {
mCurrentUser = mAuth.getCurrentUser();
String email = mCurrentUser.getEmail();
mEmailHeader.setText(email);
mUser.setEmail(email);
writeNewUser(mCurrentUser.getUid(), mUser);
}
}
private void writeNewUser(String userId, User user) {
mDatabase.child("users").child(userId).setValue(user);
}
private void initializeViews() {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyUtilty.showSnackbar(view, "Replace with your own action !");
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
mEmailHeader = headerView.findViewById(R.id.textViewEmailHeader);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id) {
case R.id.action_logout:
FirebaseAuth.getInstance().signOut();
finish();
return true;
case R.id.action_profile:
Intent intent = new Intent(this, ProfileActivity.class);
```startActivityForResult(intent, REQUEST_CODE);
return true;```
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_trips) {
MyUtilty.switchFragment(mManager, new TripsFragment());
} else if (id == R.id.nav_gallery) {
MyUtilty.switchFragment(mManager, new GalleryFragment());
} else if (id == R.id.nav_tips) {
} else if (id == R.id.nav_map) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("RESULT", "TEST");
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data != null && data.hasExtra("name")) {
String[] array = data.getStringArrayExtra("name");
mUser.setFirstName(array[0]);
mUser.setLastName(array[1]);
writeNewUser(mCurrentUser.getUid(), mUser);
Log.d("TEST", "It's working");
}
}
}
}
}
ProfileActivity
public class ProfileActivity extends AppCompatActivity {
private EditText mFirstName;
private EditText mLastName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
initializeViews();
}
private void initializeViews() {
mFirstName = findViewById(R.id.editTextFirstName);
mLastName = findViewById(R.id.editTextLastName);
}
@Override
public boolean onSupportNavigateUp() {
Log.d("NAV_UP", "Nav up working");
String firstName = mFirstName.getText().toString();
String lastName = mLastName.getText().toString();
String[] array = {firstName,lastName};
Intent intent = new Intent();
intent.putExtra("name", array);
setResult(RESULT_OK,intent);
finish();
return super.onSupportNavigateUp();
}
}
清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ProfileActivity"
android:parentActivityName=".MainActivity"
android:label="Profile"></activity>
<activity
android:name=".LoginActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignupActivity"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
答案 0 :(得分:0)
尝试将new Intent()
替换为getIntent()
-否则将没有REQUEST_CODE
。
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = this.getIntent();
String firstName = mFirstName.getText().toString();
String lastName = mLastName.getText().toString();
if(firstName != null && lastName != null) {
intent.putExtra("name", new String[] {firstName, lastName});
this.setResult(RESULT_OK, intent);
} else {
this.setResult(RESULT_CANCELED, intent);
}
this.finish();
}
return super.onKeyDown(keyCode, event);
}
答案 1 :(得分:0)
我将代码移至一个按钮,而不是使用onSupportNavigateUp()。 由于某些原因,onSupportNavigateUp不支持执行意图
public void onSave(View view) {
String firstName = mFirstName.getText().toString();
String lastName = mLastName.getText().toString();
String[] array = {firstName,lastName};
Intent intent = this.getIntent();
intent.putExtra("name", array);
this.setResult(RESULT_OK, intent);
this.finish();
}