how to restart activity from fragment? - android

I have a MainActivity which has a navigation drawer.
If I select an item in the navigation drawer, it launches a dialog fragment FragmentA. Now, if I change a few things in FragmentA, I want MainActivity to reflect the new changes once FragmentA is dismissed. What is the best way to restart MainActivity from FragmentA?

use this code
getActivity().recreate();

Your question is not clear. I think FragmentA is a DialogFragment. At least i assume it.
You can override onDestroy method in FragmentA and write
((MyActivity)getActivity()).refreshUI();
We basicly, casted activity instance to our activity for letting us call our method that you can refresh ui.
An dirty way is,
You can also write
Intent intent = new Intent(getContext, MyActivity.class);
intent.setFlag(Intent.CLEAR_TASK);
startActivity(intent);
By this way, we started our activity again and killed the one which is in backstack. I assume your datas is hold from another class which like singleton. Otherwise you lose them or you can use first method.
Good luck there.

Related

Refresh activity with fragments in back stack

I'm working on the application with multi language support. I need to be able to change application language runtime so after updating the context with new locale I need to restart the activity. This activity draws fragments so before the restart there are few fragments in back stack.
I found recreate() method in Activity class which works fine. Problem is that this method blink the screen which looks bad.
I also found another approach how to refresh an activity. This is without a blink:
finish()
overridePendingTransition(0, 0)
startActivity(intent)
overridePendingTransition(0, 0)
Problem with this is that it removes all fragments from back stack.
Is there any other approach how to refresh the activity with fragments without a blink?
use async task
1- use dobackground method
2- use dopost method

Android going back to previous activity instead of deleting fragment?

I have 2 activities with fragments inside of them. Whenever I press the back button, the fragment gets destroyed (Used FragmentTransaction's replace method for adding them into the activity) instead of going back to the first activity immediately.
How can I achieve the behavior I want to have?
You can override onBackPressed method in your activity and check if the fragmentManager.getBackStackEntryCount()==0. If yes finish() the activity.
Hope it helps.

How to share fragments through activities?

I need a way to share Fragments through different activities, so for example I have MainActivity with a Fragment and when I go to SecondActivity I need that same Fragment loaded, but the Fragment can vary so it would not always be the same one.
I've guessed that I could get the actual Fragments id or tag and pass it on the Intent so I could retrieve it on SecondActivity and use it to load the correct Fragment, but I don't know how.
You can't. You have to create a new instance for the fragment and load again the data in it. If you created the fragment in a good way, it is a really simple task. The reasons why you can't reuse the fragment are the following:
If you could do something like that, what would happend to this fragment's lifecycle? What about going back from current activity to the other? Everything can be messed up.
Every activity has its own FragmentManager and they are never shared between activities, so you can't ask in another's activity for a fragment that doesn't belongs to it.
If you have some doubts on how to pass data using intents between activities to tell the fragment what to load, have a look at this post.

start activity inside fragment without button click. Android

How do i start an activity inside fragment without button click?
I want to directly start a activity inside fragment without clicking the button.
I have searched for answers but every answer is related to button click.
Please help. I am new to android.
override oncreateView() method and in that
Intent i=new Intent(getActivity(),yourActivity.class);
startActivity(i);
Let me clear about Activity & Fragment. Activity is basic android component which can hold many Fragment. Fragment can not hold any Activity. For example : MyActivity is an activity which may hold MyFragment1 & MyFragment2. Now you can go from Fragment to another activity not MyActivity because MyActivity is already running & holding fragments. If you want to go MyActivity2 from fragment then write simple activity starting code. I am giving it bellow.
Intent intent = new Intent(getActivity(),MyActivity2.class);
startActivity(intent);
You can write it onCreateView of fragment.

Understanding the fragment and activity lifecycle and backwards navigation

I'm trying to understand some odd behavior. I have an ActivityA that calls a method in onCreate() to add FragmentA to R.id.fragment_container. Inside FragmentA I have a button that attaches FragmentB by using ActivityA's fragment manager (getActivity().getSupportFragmentManager()) and replacing the R.id.fragment_container and I also add it to the backstack. I also have another button that starts a new ActivityB.
When I navigate back from ActivityB I get: ActivityA onResume(), FragmentA onResume(). But when I navigate back from FragmentB I get: FragmentB onCreateView(), FragmentB onActivityCreated() then the 2 onResume().
So my questions is...why is the view state saved when a new activity is launched and not when the fragment is replaced and reattached. It looks much better to just restore that state rather than recreate the views and fetch that data again. This seems like opposite behavior from what I would expect so I'm clearly missing some fragment state saving/restoration step or something. It seems like the activity is just pausing FragmentA (and ActivityA) when ActivityB is launched and restoring it on back pressed but when FragmentB is attached FragmentA gets completely destroyed. I'm sure there's a way to prevent this I just can't seem to figure it out. Thoughts?
Just below your question are four tags android,android-fragments,android-lifecycle& android-navigation .Put your cursor over it for a while a black box will pop up . click on info tab and you will get best links to study that topics along with links to books .
Hope this will help you

Categories

Resources