Scenario:
Base class which includes Navigation drawer layout having header section as in usual apps. Header includes an imageview and textview. There's another activity lets say Profile which is extending from Base class in my case and I don't want to change this parent/child behaviour at this point.
Problem:
Profile activity has api call which updates the profile image on server. On getting success in response, I just want to update the navigation drawer ImageView
regardless of user goes back to other activity or remains at Profile activity.
What I have tried so far are:
Declare ImageView
in Base Activity as static and try to use Picasso to update image in Profile activity api response.
Declare ImageView
in Base Activity as static and tried imageview.setBitmap()
method.
Tried to use instance of Base class in Volley success method and use method of base class where I am trying to update ImageView
.
Tried to use runOnUiThread()
in api success response but of no success.
This doesn't make sense in my case as the user might not land on base activity while pressing back button. Below is the piece of code in Profile Activity where I am trying to update image in Base Activity
private void uploadImage() {
if (bm == null) {
uploadProfile();
}
else {
String REGISTER_URL = new SharedPreferenceManager(getApplicationContext()).getKeyFromSharedPrefFile("MySharedPrefs", "globalURL") + "users/mobileupload";
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
navBarImage.setImageBitmap(bitmap); // where navBarImage is declared in BaseActivity Layout Navigation drawer in header layout
}
},
....
requestQueue.add(stringRequest);
}
}
Api call is working fine , image is uploading and updating smoothly if I logout and login again.
Now my question is whether there is a way to be able to update the view of parent class instantly ... and if yes, can you provide some example.
As far as I understand the SecondActivity
sets the information including adding an Image. So the goal of the task is to automatically update the FirstActivity's
Drawer's ImageView
. We cannot just call the view in the SecondActivity
because the activity should work independently (which means -- they must not know each other).
I believe it is a great usecase for onActivityResult()
since we want to pass back a result from the SecondActivity
. The information we need to pass is a Uri
-- the uri represents the Image location in the device.
SecondActivity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_FILE && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
// We will pass the value at the previous activity using this code
Intent responseValue = new Intent();
responseValue.putExtra(PROFILE_URI_KEY, filePath);
setResult(Activity.RESULT_OK, responseValue);
}
}
The SecondActivity
recieves a Uri
from the CameraActivity
, that Uri
must also be pass back to the prev. Activity so that it can perform the invalidation of Drawer'a Image.
MainActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e(TAG, "We receive someting " + requestCode + " " + (resultCode == Activity.RESULT_OK));
if (requestCode == RESUL_KEY_FOR_EDIT && resultCode == Activity.RESULT_OK) {
Uri uri = (Uri) data.getParcelableExtra(EditProfile.PROFILE_URI_KEY);
Picasso.with(getApplicationContext())
.load(uri)
.fit().
centerCrop()
.noFade()
.error(R.drawable.placeholder)
.into(navBarImage);
}
}
In our MainActivity
we are using Picasso for image loading due to improvements in performance.