1

I have two classes:

One is Fragment1 which containts fragment - like this:

public class Tab extends FragmentActivity

the second one is activity - like this:

public class Wallpaper extends Activity

Now I want to start Activity from Fragment1 how can I do this ?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
  • To call activity from fragment, checkout http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity . However, your class Tab seems to be not a Fragment, but activity. – sandrstar Oct 30 '13 at 03:25

4 Answers4

3

I want to start Activity from Fragment1 how can I do this ?

You have this

public class Tab extends FragmentActivity in which case you can use the below unless you are starting activity from fragment class

 Intent intent = new Intent(Tab.this,Wallpaper.class);
 startActivity(intent);

Use the below in your fragment

  Intent intent = new Intent(getActivity(),Wallpaper.class);
  getActivtiy().startActivity(intent);  

getActivity

Return the Activity this fragment is currently associated with.

http://developer.android.com/guide/components/fragments.html

There is an example in the docs check the same

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • well when i m using this line "Intent i = new Intent(getActivity(), Activity.class); startActivity(i);" i am getting error at getActivity()..like create methid for getActivity(); – Ando Masahashi Oct 19 '13 at 09:08
  • @user2783386 that is bcoz your using it in `Tab` class in which case use `Intent intent = new Intent(Tab.this,Wallpaper.class); startActivity(intent)` and `Tab` class does not extend `Fragment`. `FragmentActivtiy` is Base class for activities that want to use the support-based Fragment and Loader APIs. – Raghunandan Oct 19 '13 at 09:14
  • @user2783386 i doubt whether you have a class that extends `Fragment`. – Raghunandan Oct 19 '13 at 09:18
  • well forgot every thing i have frame-Layout and i want to show wallpaper class then how can i do ? – Ando Masahashi Oct 19 '13 at 09:27
  • @user2783386 why do you need a frame-layout. use fragmentactivity to host a fragment which is in the support library. – Raghunandan Oct 19 '13 at 09:53
  • well my porpose is when some one click on action bar then i have to show different activity and in action bar i have three button that's why – Ando Masahashi Oct 19 '13 at 09:58
  • @user2783386 you are getting it all wrong. You have tabs not buttons i assume and you need to navigate to show the content of a different xml on each tab selection. that is what you want right. and pls be clear with your next qiestion that you asked – Raghunandan Oct 19 '13 at 09:59
  • @RahulGupta just bcoz i commented and flagged your question you unaccepted the answer that is not good. Certainly i am not going to answer your questions in the future. what a waste of time – Raghunandan Oct 29 '13 at 11:06
  • y are you downvote at my question ? thats why if u want to answer then give your answer – Ando Masahashi Oct 29 '13 at 11:13
  • @RahulGupta i had commented first only after you commented i flagged and downvoted and if i know i will certainly answer. It was downvoted coz it was a duplicate of previous question asked – Raghunandan Oct 29 '13 at 11:15
  • @RahulGupta and check this before coming to conclusions http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled. – Raghunandan Oct 29 '13 at 11:18
  • Duplicate questions are not necessarily bad; different descriptions of the same problem help future visitors to find the answers they're looking for. However, asking a duplicate question may – Ando Masahashi Oct 29 '13 at 11:20
  • @RahulGupta tell me if it not a duplicate i will remove the downvote but even i do others will still downvote and close the questions. but it certainly looks exact duplicate. but yours is exactly duplicate i see no change except for few words which are too minor. if you still have a problem bring it on meta.stackoverflow.com – Raghunandan Oct 29 '13 at 11:21
  • fine bro if u have solution then provide here – Ando Masahashi Oct 29 '13 at 11:22
  • @RahulGupta if i know i will certainly help you can delete the question and your downvotes will be reversed. and remember not to ask duplicate questions if you don't find answers – Raghunandan Oct 29 '13 at 11:23
  • if you seen my question then hwy are you not answered there ? – Ando Masahashi Oct 29 '13 at 11:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40174/discussion-between-raghunandan-and-rahul-gupta) – Raghunandan Oct 29 '13 at 11:27
2
Intent intent = new Intent(context, Wallpaper.class);

// intent.putExtra(....); // put your data

startActivity(intent);
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
0

See here: Android : Calling Activity from Fragment - the first result on Google searching for your question title...

Community
  • 1
  • 1
Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

try this

Intent intent = new Intent(getActivity(), mFragmentFavorite.class); startActivity(intent);

Mahendran Candy
  • 1,114
  • 17
  • 17