I have a problem. According to the drawing, I have an activity that has three fragments, each fragment has its recycleview with the items to choose from, so far so good.
My goal: when I click on an item, it opens another activity, legending it with a variable, so far so good.
When I press the back button, I leave the second activity and go back to the first with the variable, the problem is when I return to the first activity, I tried to trigger a function that analyzes the variable within onResume (), but this function it doesn't work, I repeat, it doesn't work with fragments. What return function works on fragments? Is there another simple alternative?
See the image here
Start your child Activity for result using startActivityForResult() and then in your child Activity set the result, after you have collected your data, using setResult() and manually finish your child Activity using finish(). Then in the Fragment class again, override onActivityResult() to receive the data set in your child Activity.
Refer to this guide for more in depth information: https://developer.android.com/training/basics/intents/result.
Related
I'm new with Xamarin and I'm tring to create an Android App.
I have created a 2 Views related to 2 different Activities. The first Activity, name it A, has a button that launches the second Activity, name it B.
B has an EventHandler that is connected, in the OnCreate method, to A's Event. The EventHandler print on console a string, that's it.
if I launch the app and press the A's button, the B activity appears. Now, if I press the Back button the A Activity appears again, after that I press the button again and the B Activity appears but this is a new B Activity istance and not the previous one. I can see that because the OnCreate method is called twice and because I can see on console that the event is called twice.
If I repeat this many times I can see the same string printed on console repeated many times. I would like to have only one instance of any activity, so I need to change view without creating more istances of one activity that was already created or destroy the B Activity when the Back button is pressed.
How can I do that? is it the right way to do it?
You can't link the two activities, or better, you really shouldn't. If you need shared data then go through static properties (perhaps in a separate static class).
Read about activity lifecycle and activity launch modes. Note that you are probably creating a wrong flow though.
I think in order to get what you want (having two "Activities" of which the instance is always the same when you are switching between them), you basically need to use Fragments instead of Activities, in a FragmentPagerAdapter.
http://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/
FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)
I have an application with two activities. The first contains a tab bar with three fragments in it. Clicking on a button in the fragment loads the second activity.
When the second activity is dismissed, either by the back button or by the Up Navigation, I need to refresh the data in two list views in the main activity's fragments.
What is the best way to do this?
The key is understanding the Activity and Fragment lifecycles. When the first Activity and Fragment return from to the foreground several events are called such as onResume() this is one spot your refresh could occur at.
You need to start your second Activity with startActivityForResult, and in onActivityResult, refresh your fragments.
When your second activity dismissed, the first one brought to front, right? This causes one of the tab fragment to show again. In your fragment you probably have a List Adapters for your list views.
Try this in your fragment:
#Override
public void onResume() {
super.onResume();
mListAdapter.notifyDataSetChanged();
}
As Android newbie I started to wonder about the Activity lifecycle. I'm having an Activity that loads a list of Persons and displays them. Upon the click of a Person I want to open another Activity showing the details of that Person. I'm currently doing this by creating an Intent on the "PersonDetailActivity" which I then start. So far so good. On the PersonDetail page I would like to have a menu action to go back to the Person list. I again applied the same technique, meaning an Intent that brings me back to the PersonListActivity.
Now I started to wonder what returning to the PersonListActivity means. Will a new instance get created and will I have to reload the persons that it displays in the list? Can you come back to the same instance, avoiding having to reload the list again? Do you then have to pass a pointer to yourself via the intent to the other Activity?
So when will and Activity be re-instantiated and when will it not. Any hints or suggestions are more than welcome. Maybe there are some patterns to be applied for these back and forth menu actions that I'm not yet aware of.
Thanks,
Vincent
Yes,,. Call finish() in second Activity instead of starting new Activity..
There is basically something called Activity stack which stores all Activities in the order they were started.. so if start new Actvity , that sits on top of the stack and preveous one gets below it.. when you call finish the Activity is poped out..
if you don't want to call finish() correct waht ever you were doing then add flag ACTIVITY_CLEAR_TOP in manifest for the 1st Activity..
Basically if you just call the finish() method on your PersonDetailActivity
PersonDetailActivity.this.finish();
it will activate the onResume() method from the Activity that is on the top of the finished one, which here would be your PersonsActivity. You can specify in your onResume() method what you want to perform when turning back there.
Pseudocode :
The initial Activity starts. (ActivityStartScreen)
A button is pressed that starts a new Activity (ActivityOtherScreen) by using an Intent.
The new Activity has a button that loads the initial Activity.
My question is, is the original activity gone once it 'loses focus' (when the 1st button is pressed), or is it stored somewhere, and is there a way to retrieve it?
Currently, Im re-creating the original activity with an intent. I bet this isnt the proper way.
The previous Activities are stored in the Activity stack, to return to previous activity, just call finish() on current activity. Note that this way you lose the 2nd activity.
You simply must call the finish() method from your second activity to go back with the states all being the same.
Here is a helpful warriorpoint blog post tutorial that will walk you through it.
I'm making an app which has a flow roughly as below:
User starts on the main screen with an empty list, hits menu, and goes to "add item." (Activity A)
User is given a new activity which allows them to specify search criteria, then hits "go" to do a search. (Activity B)
User gets a list of results, and can click on one of them to view more details. (Activity C)
User sees details of item, and can use a menu item to save it to their list in Activity A. (Activity D)
Right now, I am having each Activity call each other Activity for results, and then it is passing the result all the way back up the stack as it returns to Activity A.
Is there a way to jump this, since all I want is for a result in Activity D to get to Activity A directly?
Note that a user should still be able to navigate backwards (using the back button) through each activity, but if they explicitly save the item in Activity D, I want it to jump straight to Activity A.
I recommend just invoking the activities (not using the *ForResult) calls, then having activity D invoke Activity A with an INTENT_ADD_ITEM with data, then have Activity A add the item.
Hope this helps...
Just so that people can benefit from what I learned later...
The key to solving this problem is using flags with Intent, in this case using FLAG_ACTIVITY_CLEAR_TOP. Other flags are useful as well in controlling the flow of your UI.
It is a bad idea to try to solve this problem by chaining startActivityForResult() through activities. It means that it's difficult to change the flow of your application.