I have seen this Question but didn't get good answers and my situation is a little bit different. I have 2 activities A and B. From Activity A I launch Activity B but activity A must stay opened, now after I login in Activity B I save some data in Shared Preferences and I need the layout in Activity A to change according to the shared preferences values, so after login Activity A must recreate to apply changes but still should be behind Activity B.
So my question is it possible to recreate Activity A from Activity B??
I don't know what exactly you mean with "activity A must stay opened" though so you'll have to figure out that part or give a more detailed explanation. Do you mean Activity B overlays Activity A? I'll assume that.
I suggest you to use 3 fragments in Activity A. One fragment for the initial layout and another one for the new layout. Then another fragment instead of Activity B, I'll call it FragmentB.
In FragmentB
create a Callback inner interface.
public interface Callbacks {
void onLoggedIn();
}
Then make Activity A implement this callback
public class ActivityA extends Activity implements FragmentB.Callbacks {
//rest of the activity...
@Override
public void onLoggedIn() {
getFragmentManager().beginTransaction()
.replace(R.id.container, new SecondFragment())
.commit();
}
}
Obviously you can choose any name you like for ActivityA
, FragmentB
, Callbacks
, onLOggedIn
and SecondFragment
.
Where SecondFragment
is the name of the fragment that contains the layout after being logged in in FragmentB
.
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Instead of **recreate* your activity, you should use startActivityForResult
and update your activity on onActivityResult
e.g.
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With