How do i change a listview on activity resume? - java

I have a ListView of database items in my main activity, in my main activity i have a method that populates the list view from my database that runs on the oncreate of the main method.
from my main activity the user can go into the menu and click on something called "change view" which is an activity that lets the user change the search criteria for the database, I then have a method that populates the listview from this search criteria.
After the user clicks on the change view button within this activity i want the application to then go back to the main activity and to display this new custom list view, my first thought was to call this method in the on resume, but that doesn't work for the obvious reasons, would appreciate any direction.

you can use startActivityForResult from your main activity or the activity with the list to call the change view activity and then pass the selected option back to the calling activity and use the onActivityResult to read the data returned
and then populate the listview based on it
this link will give you a starting point.or you can use the official reference

You can use custom broadcast receiver for that like below
When you click on the button write this code
Intent i = new Intent("android.intent.action.MAIN")
this.sendBroadcast(i);
finish()//finish your current button activity if this is an activity
Now in onResume of listview activity
IntentFilter intentFilter = new IntentFilter(
"android.intent.action.MAIN");
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Update you listview
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
And in onPause you must unregister the receiver like below code
this.unregisterReceiver(this.mReceiver);

Related

How to maintain binding on Fragment -> Intent -> Back to Fragment

I am using a Fragment and within my Fragment, I am starting a new Intent to access the device's settings. When I select the 'Back' button from the Android device, my bindings do not reflect in my View. The ViewModel properties are correct when I debug but the View doesn't reflect live data updates anymore. This only happens when I open the Intent
I am opening the Intent here:
private View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
I am using the ObservableFields in my ViewModel so I think it's working correctly as I mentioned before - the data is correct on the debugger, just not reflected in the View if I go into the Intent. The View will reflect any changes if I do not go into the Intent. Do I need to Override the onResume method?
As the doc says,
the fields should be declared final in your data model because bindings only detect changes in the field's value, not of the field itself
Please check this statement. If if didn't fix your problem, can you post some additionnal code as your data model class, and how you use it in your fragment?
Hope it helps.

Update an activity after a dialog is dismissed

In my main activity I have an alert that allows users to edit a setting. Once the user selects that setting and it's dismissed I want to call the onCreate() method to refresh the main activity to be updated with the new settings but I'm not sure how to call the main activity from inside the onClick() function.
The dialog is created in a method in the main activity. I could also call a refresh() method I've created inside my class to update everything but that has the same issue of calling a non-static method from a static context.
Try this :
MainActivity.recreate();
The activity will be recreated with new instance so It will recall onCreate again as you needed. It is supported for API 11 or higher.
For APIs lower than 11 use this:
Intent intent = getIntent();
finish();
startActivity(intent);

In android Studio, I want when i click on button , next activity/fragment should come from right side

In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}

Returning to the proper activity when pressing the BACK button?

I'm creating an app with multiple screens the user will have to navigate through. Specifically, I'm currently working on a set of activities that must link together as follows:
Main Activity -> a button click leads to "CreateCharacterActivity" -> a button click leads to "CharacterMainActivity"
The BACK button on "CharacterMainActivity" should lead back to MainActivity without showing the CreateCharacterActivity again.
This behavior should be similar in other areas of the app, except it should restore the state the activity the BACK button leads to was in before it was paused.
So to simplify, I want it like this.
Activity A -> Activity B -> Activity C
BACK button causes Activity C to return to Activity A without going through Activity B.
I tried doing this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
But this just invokes a new instance of MainActivity. When I then press BACK at that MainActivity instance, it takes me back to CharacterMainActivity.
How can I achieve this? I'm assuming it involves accessing the Activity stack?
When you move from Activity B to Activity C, call finish() at the same time you call startActivity on Activity C. This will remove Activity B from the task stack.
You can start the child activities for result and then finish the child activities with the result also with the same request code.
To start the child activity:
startActivityForResult (Intent intent, int requestCode)
for finish all the child activity
finishActivity (int requestCode)

How to return control to main activity (basic android app)

I'm still quite new to android programming. I currently have a button on the home page that takes the user to a second activity. On the second activity, the user fills in two EditText boxes and clicks a button at the bottom of the page that should send information that the user entered with it. The button that returns to the main activity uses the following code:
Intent returnIntent = new Intent();
String strData = "your data";
EditText foodName = ((EditText) findViewById(R.id.foodLabel));
String foodNameString = foodName.getText().toString();
returnIntent.putExtra("data", foodNameString);
setResult(RESULT_OK, returnIntent);
finish();
Where would I put code in my main activity that should run as soon as this button is clicked? And how can I access the information I sent back? Right now it returns to the main activity but does nothing.
If you expect a result from your second activity, you need to start second activity via startActivityForResult() (not the startActivity()). In this case, once activity you launched is finished, Android will trigger onActivityResult() callback within your main activity. All the data you specified via setResult() will be passed in this callback

Categories