So, my application have one activity ProjectActivity as a home, which contains 3 fragments HomeFragment ListFragment TagFragment(one for each tab). When I click on a button (on the homeFragment), it opens a new activity CaptureActivity.
That activity launches an ASyncTask, ParseData.
On the ParseData onPostExecute, I would like to go back to my ProjectAcitity, focused on a specified tab (the one that contain ListFragment), and update the ListFragment View.
I actually have no clue on how to do that. Do I simply need to call the ProjectActivity ? But then, if I press the Back button, wouldn't it go back through the whole chain (Project Activity, CaptureActivity, ProjectActivity) ?
Well, you can do a few things.
First just have ListFragment monitor w/e its listing and refresh it in its onStart/onResume which will be called when you return to your ProjectActivity.
Or more directly you could do startActivityForResult(captureActivity); then when u come back to your ProjectActivity get results in onActivityResults() then update your ListFragment from there.
Related
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.
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 four fragments in one viewpager of Activity1, In any of fragment1~4, I open another activity2 by startactivity(Intent), In activity2, I want to return back to previous fragment or other specified fragments, but Now I always return back to the first fragment1 by startactivity(), Any good ideas ? Thanks
You can access the current 'page' and set the current page like this. I guess you could bundle this information when you start activity2 through the intent. Note "FirstFragment" is the name of the activity that sets up my view pager etc with fragments.
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
Once, in activity 2 and you want to return back to it, or another view pager (currentviewnum), you could do something like this in an override for the back button (see here for that Android - How To Override the "Back" button so it doesn't Finish() my Activity?); But you can do something like this!
((FirstFragment) getActivity()).getPager().setCurrentItem(currentviewnum);
So effectively when you press the back button, let me know if this works. I do something like this in my app. But instead of going back, it's more to go to the next one!
Let me explain my application first.
I have an activity and fragments in it. I'm opening fragmentA onCreate of application. In fragmentA,before i open fragmentB by using support fragment manager's replace() method, i call a webService for some json datas.
Then i open fragmentB with these json datas. After i opened the fragmentB, i want to turn back to fragmentA by using device's default backbutton, and i don't want to handle backButtonPress(). Just go back to previous fragment. My app does this.
Here is the question. When i press back button, my fragmentA calls the service again and it affects the user to wait. But how can i just turn to the previous fragment ?
I am looking for the best solution to turn previous fragment.
In my android apps i have four activity A->B->C->D and in every activity i call web service then update the list-view and other element from the server so suppose i did delete some data from the database for Activity A and if user access the Activity B and he press the back button then Activity A should be refreshed. I did override onResume method and check the data is updated or not but didn't worked. I also tried another way but not achieve my goal.
my Activity structure is like a following
Class A Is Fragment which is having two tab and each tab contain listview
Class B Is Activity
Class C Is Activity
Class D Is Activity
I don't understand how to call web service again when press the back button and how to update fragment and activity when user press the back button. Please anybody suggest me some answer.
Thanks in advance
you can override the OnBackPressed-method. check the top-answer on that link!!
Android: Proper Way to use onBackPressed() with Toast
Start activity B from activity A with "startActivityForResult" instead of "startActivity".
In activity B you defaultly return RESULT_CANCELED -> so setResult(RESULT_CANCELED). Only when activity B is finished on purpose you return RESULT_OK.
In activity A you then just need to implement onActivityResult(). Depending on the result returned from activity B you can then do specific stuff in activity A, in your case refresh something.
Here is an example: http://www.vogella.com/articles/AndroidIntent/#usingintents_sub