Adapter values is not updated from another activity - java

I am stuck with a problem where I need to update the value of a adapter variable in another activity and coming back to first activity should also get that updated value.
my current flow is like. I am staring the BarDetailsActivity and passing the modal with intent from inside the adapter class like:
Intent barDetailIntent = new Intent(getApplicationContext(), BarDetailActivity.class);
barDetailIntent.putExtra("isfav", barsList.get(position));
barDetailIntent.putParcelableArrayListExtra("barlist",barsList);
mContext.startActivity(barDetailIntent);
Then on another activity I am getting that model from intent and changing its variable values as:
gbar = in.getParcelableExtra("isfav");
blist= in.getParcelableArrayListExtra("barlist");
if (gbar.getmFavourite()) {
gbar.setmFavourite(false);
} else {
gbar.setmFavourite(true);
}
Now on going back to my main Activity Value for "gbar.setmFavourite" is not updated on onresume of the MainActivity.
#Override
protected void onResume() {
super.onResume();
if(mAdapter != null){
mAdapter.notifyDataSetChanged(); // here the adapter value is not updated.
}
}
Please help me on this.

Use Event Bus to handle the problem.
Register your First Activity to listen the Event and override
onEvent method.
Fire the stickyIntent with updated dataset from SecondActivity.
In the onEvent method of the FirstActivity call notifyDataSetChanged
with the updated dataset.

Related

Sending a recorded audio file from one activity to another

I am currently recording a message and initialising the objects in one fragment. I'd like to pass it to my main activity. However, I can pass an image between fragments using this:
public void captureImage() {
mCamera.captureImage(new CameraKitEventCallback<CameraKitImage>() {
#Override
public void callback(CameraKitImage cameraKitImage) {
((MainActivity) getActivity()).setBitmapToSend(cameraKitImage.getBitmap());
((MainActivity) getActivity()).openDisplayImageFragment();
}
});
}
I have initialised a media recorder within the class. Is there any way to do this?
If the Main Activity is hosting your fragment, then you can send the data from fragment to its parent Activity by - Interface callbacks or creating a public method in your Activity.
Since you can always get the reference to the parent Activity using getActivity(), you can cast it and pass values to the public method. Eg:
myActivity=(MainActivity) getActivity ();
myActivity.receiveAudio(<"Pass the data as object or in byte[]">);
Sending data from a fragment to another activity require to add Intent extras. Eg:
myIntent.putExtra("myKey",<"Place your object or byte array">);
startActivity(myActivity);

How to call function containing intent (in Activity A) from Activity B

I am Calling a function(present in fragment) from activity when back button is pressed
public void onBackPressed() {
new Home().show(Home.home_list,app.this);
}
Home is fragment of app activity
In show function I am calling an intent
public void show(final ArrayList<tile_data> data, final Activity activity) {
startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
}
In doing so it gives me error : java.lang.IllegalStateException: Fragment Home{5deabab} not attached to Activity
My conclusion is that app is using show as function so it doesn't know about home.
So my question is how to call an "intent" present in some function in some activity or fragment from other activity??
Looks like startActivity is not being called in the right scope. Try specifying the scope and calling it like this: activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
Why don't you move the Show() function to the Activity? And for the titles create a getter function in the Home fragment, getTitles(). Now, when the event occurs trigger the Show() function directly in the activity passing (*HomeFragment*.getTitle() , *Activity*.this). Are your titles static? Because you are directly calling new Home(). If that's the case there won't be any issue in calling Show() function in the activity. You can explain your requirements in detail if this isn't up to par.
public void show(final ArrayList<tile_data> data, final Activity activity) {
activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class));
}

How to finish the main activity from other Activity

Hello how do you finish Main activity
Assume that there are 3 Activities and 1 Fragment
LoginActivity , MainActivity, infoFrgMent, ChangePwdActivity.
The scenario is when I loggedin in LoginActivitythen MainActivity will show up LoginActivity will finish() then i will go to my info which is 'infoFrgMent' then i want to change my password after i changed my password.
LoginActivity will shows up Again to relogin but whenever i try to press back MainActivity shows up and didn't finished.
You need to remove previous activities form stack
setFlags to intent from MainActivity to LoginActivity
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
refer : https://developer.android.com/guide/components/activities/tasks-and-back-stack.html
What you need is to add the Intent.FLAG_CLEAR_TOP. This flag makes sure that all activities above the targeted activity in the stack are finished and that one is shown.
Another thing that you need is the SINGLE_TOP flag. With this one you prevent Android from creating a new activity if there is one already created in the stack.
Just be wary that if the activity was already created, the intent with these flags will be delivered in the method called onNewIntent(intent) (you need to overload it to handle it) in the target activity.
Then in onNewIntent you have a method called restart or something that will call finish() and launch a new intent toward itself, or have a repopulate() method that will set the new data. I prefer the second approach, it is less expensive and you can always extract the onCreate logic into a separate method that you can call for populate.
To finish another Activity you have to create a static method to finish this Like here:
MainActivity.java
private static MainActivity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
activity = this;
}
public static void finishThis()
{
try
{
if (activity != null)
{
activity.finish();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
And call it like this:
AnotherActivity.java
MainActivity.finishThis();
That's it

Call a Method from another Method Android

I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference

keeping data updated between two activities in android

How can I keep data updated between two android activities. I am working on a android album project where the mainactivity is album activity(where I can add remove and rename albums). But when the user clicks on one of the albums just created I start a intent with a photo activity where he can add remove and move photos. All the operations are done for the album in the photoactivity and I use serialization to write the data back to a text file in both activities. The problem is when I back out to the main activity from the photo activity making some changes on a particular album. The mainactivity doesn't know about the updates.
Code in main activity to start the photo intent on a particular album selected by user
albumGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent photoIntent = new Intent(MainActivity.this, Photo.class);
photoIntent.putExtra("userNode", user);
photoIntent.putExtra("position", position);
startActivity(photoIntent);
// Toast.makeText(MainActivity.this, position, Toast.LENGTH_SHORT).show();
}
});
You see I pass in the user object which is linkedlists of all album and photo nodes which added removed or moved. It is basically the entire user data of the that user. So whenever I start the old reference of the user node is passed on to the photo intent. I have implemented readerUser() and writeUser methods using serialization. I need to keep the reference of the user object in the main activity updated with all the changes in the photo activity..
Use contentProvider to provide unique access to your photo data and implement an Observer design pattern. That is , in on side, inside the the ContentProvider, when dataset changed due to insert ,update,or delete, notify the the contentResolver;on the other side, user has to register the notification by calling getContentResolover().registerContentObserver
Check out those links:
http://developer.android.com/reference/android/content/ContentProvider.html
http://developer.android.com/reference/android/content/ContentResolver.html
http://mylifewithandroid.blogspot.com/2008/03/observing-content.html
Look into implementing an Android Application. The Application class essentially gives you a GUI-less activity that remains constant for the duration of your session. Instead of serializing objects (slow, excess overhead), you can just call a method on your Application to save images to a data structure in the Application.
Using an Application means that any of your normal Activities can obtain a reference to the Application singleton and access any field or method which you expose.
Read the offical doc and do a Google search for some implementation examples, of which there are many.
I think what you need is database. Use sqlite http://developer.android.com/reference/android/database/sqlite/package-summary.html .
That is guessing you also want the data persisted and restored when the application is run a second time.
Implement broadcast receivers.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
I used as #Vegito1044 proposed a local BroadcastReceiver in the main Activity that can be triggered from other activities. The relevant code for this looks like this:
public class AlbumActivity extends Activity {
private BroadcastReceiver myReceiver = null;
class _AlbumUpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive (Context context, Intent intent) {
Log.i(Global.LOG_CONTEXT, "AlbumActivity.onReceive(intent='" + intent + "')");
reloadGui();
}
}
#Override
public void onResume() {
super.onResume();
if (myReceiver == null)
{
myReceiver = new _AlbumUpdateReceiver();
IntentFilter filter = new IntentFilter(Global.REFRESH_GUI);
registerReceiver(myReceiver, filter);
}
reloadGui();
}
#Override
public void onPause() {
if (myReceiver != null)
{
unregisterReceiver(myReceiver);
myReceiver = null;
}
super.onPause();
}
void reloadGui()
{
Log.d(Global.LOG_CONTEXT, "AlbumActivity.refreshGui()");
... do what is neccessary to update gui
}
}

Categories

Resources