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.
Related
I have my code of first activity:
and a second activity:
but I am not sure why the part "onActivityResult" is not functioning to show the gallery?
You are starting Gallery Activity incorrectly in on Click callback in first Activity. In order to start one Activity from the other, you should use startActivity method, instead of instantiating object:
startActivity(new Intent(this, GalleryActivity.class));
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.
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
How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.
I have 3 different activity namely, category,sub_category and list of items. In sub_category i created back button using intent Activity. Back button in list_of items should be focus to sub_category. i dont know how to do this.. pls guide me..
from category activity
call
Intent it=new Intent(category.this,sub_category.class)
startActivityForResult(it);
similarly
Intent it=new Intent(sub_category.this,list_item.class)
startActivityForResult(it);
and in onClickListener of back button simply finish your activity like
list_item.this.finish();
for going back to previous screen u can call finish() in your current activity. It will remove the current activity from activity Stack and take you to the previous one.