Reputation: 595
I have two Activities 'A' and 'B' in my Android application.
1) For the first time I'm going to Activity 'B' from 'A'
2) In Activity 'B' I have 2 list-view and whenever I perform onItemClickListener
of both list-view I have store the boolean values in preferences.
3) After that when i want to go back to Activity 'A' , I want top retrieve the preferences values in Activity 'A'.
I have tried lot of but not work perfectly
Here is my some code in
Activity 'A' in onCreate()
method
booleanValue_one = sharedPreferences.getBoolean("LISTVIEW_EVENT_ONE", false);
booleanValue_two = sharedPreferences.getBoolean("LISTVIEW_EVENT_TWO", false);
Log.e("", "booleanValue_one=" + booleanValue_one + " booleanValue_two=" + booleanValue_two + " booleanValue_three = " + booleanValue_three);
Activity 'B' onBackPressed()
code
@Override
public void onBackPressed() {
super.onBackPressed();
Intent a = new Intent(B.this, A.class);
startActivity(a);
overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
}
Can someone help me how to update my previous Activity 'A' with onBackPressed()
of Activity 'B'
Thanks in advance.
Upvotes: 0
Views: 79
Reputation: 6067
Put your code in OnResume in Activity 'A' b'z it is going to call again when you press back from activity 'B'. Activity 'A' going to resume again. From Activity 'B' just finish activity so Activity 'A' won't create again.
private void openActivityB(){
// open activity code in activity 'A'
}
@Override
public void onBackPressed() {
// in Activity 'B'
finish();
}
Hope it will help.
Upvotes: 0
Reputation: 2683
Try onResume()
Kill Activity B on back pressed.
@Override
public void onBackPressed() {
super.onBackPressed();
finish
}
And on resume on Activity A
@Override
public void onResume() {
super.onResume();
booleanValue_one = sharedPreferences.getBoolean("LISTVIEW_EVENT_ONE", false);
booleanValue_two = sharedPreferences.getBoolean("LISTVIEW_EVENT_TWO", false);
Log.e("", "booleanValue_one=" + booleanValue_one + " booleanValue_two=" + booleanValue_two + " booleanValue_three = " + booleanValue_three);
}
Upvotes: 1
Reputation: 5375
while calling activity b from activity a do it with startActivityForResult()
startActivityForResult(pickContactIntent, ANY_REQUEST_CODE);
override this method in activity A
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ANY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Do whatever you want. This will be a kind of callback you will get in Activity A whenever your activity B is closed
}
}
}
in onBackPress() of activity B do
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
if you want to send any data back to activity A, you can put those values in the returnIntent and later in activity A, you can retrieve them through intent.
Upvotes: 0