Update an activity after a dialog is dismissed - java

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);

Related

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 do i change a listview on activity resume?

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);

Android onResume not called after finish() command on different activity

I have 2 Activities. My first activity calls the second activity. Once a button is clicked in the second activity it calls the finish() method on that activity causing it to return to Activity 1. This is fine, but my problem is that when it returns none of the expected methods are called (onResume(), onRestart(), on Start()). Is there anything that I could be missing? Anything that can inhibit the call? I have logs in each of the methods making sure that they aren't called. I have also added the android:noHistory="false" into my manifest to try solving the problem, but it did not fix it.
Here is the way I call the Activity
intent = new Intent(this, ViewEdit.class);
startActivity(intent);
and here is an example of the setup of the log in the methods
public void onStart(Bundle bundle){
super.onStart();
Log.d(MYACT, "On Start");
}
Thanks for any help.

intent flag over intent startActivity(i);

#Override
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), Profile.class);
startActivity(i);
}
});
what is the difference of using this code and what difference does it do on the program compared to this doe
Intent i = new Intent(CurrentActivityName.this, NextActivityName.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
First one uses getApplicationContext() to launch the intent.
Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.
Second one uses the Activity context.
Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack).
So, Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.
Source: this answer at difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this question.
Also, reading some of above links would help:
What's the difference between the various methods to get a Context?
getApplicationContext(), getBaseContext(), getApplication(), getParent()

How to destroy an activity

In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

Categories