I am working on a CustomListView
for a fragment and CustomAdapter
extends from BaseAdapter
. In my CustomAdapter
there is button on click that button i want to move a activity but i don't know how to switch from one fragment to an activity.
Asked
Active
Viewed 4.6k times
6

Anggrayudi H
- 14,977
- 11
- 54
- 87

Deep Singh
- 147
- 1
- 3
- 16
-
1) adapter cannot have a Button or any other kind of View, 2) you cannot move an Activity 3) so what exactly do you want to achieve? – pskink Mar 10 '15 at 10:59
-
He wants to move to an Activity, I suppose and not move an activity :) – Skynet Mar 10 '15 at 11:00
-
"move to an Activity" ? you cannot neither move Activity nor move to an Activity... what you can do is to start an Activity... – pskink Mar 10 '15 at 11:01
-
3This is not a grammar class! He wants to start a new Activity. You need sleep mate :) – Skynet Mar 10 '15 at 11:02
-
@Skynet i have a textView in customadapter and from that i want to move to another activity can i move or not. – Deep Singh Mar 10 '15 at 11:15
-
You can mate, you can move anywhere. Just put your code right. set an onClickListener on your textView and call the function I posted below from that onClickListener. – Skynet Mar 10 '15 at 11:16
-
@Skynet but this code is not working in customadapter that extends from baseadapter – Deep Singh Mar 10 '15 at 11:18
-
You my friend must post some code and show us what is not working. – Skynet Mar 10 '15 at 11:19
3 Answers
22
Try this:
private void moveToNewActivity () {
Intent i = new Intent(getActivity(), DetailActivity.class);
startActivity(i);
((Activity) getActivity()).overridePendingTransition(0, 0);
}
overridePendingTransition(0,0);
means no Animation in transition.
Check This, it will give you a fair idea of how an onClickListener is used to start a new Activity, from within a Fragment.

DragonFire
- 3,722
- 2
- 38
- 51

Skynet
- 7,820
- 5
- 44
- 80
-
startActivity method can't be accessed in a fragment like you said. It has to call this method using a context which is returned using getActivity(). – cafebabe1991 Mar 10 '15 at 11:01
-
I am using the above code in a Production project with around 10000 downloads and I dont find any issues. It is supplied with a context, if you may consider reading through. – Skynet Mar 10 '15 at 11:04
-
Yes but not inside a class that extends Fragment instead of class that extends Activity – cafebabe1991 Mar 10 '15 at 11:05
-
-
1http://stackoverflow.com/questions/12074608/how-do-i-start-an-activity-from-within-a-fragment – cafebabe1991 Mar 10 '15 at 11:08
-
This is the code I suggested you to use, show us how you are implementing it? – Skynet Mar 10 '15 at 11:21
-
@skynet my bad... I donno how could I skip it. But yes that method exist in fragment class as well. Thanks :-) – cafebabe1991 Mar 10 '15 at 14:28
1
If anyone is looking for this in Kotlin with databinding, perform this within your Fragment class:
binding.openActivityButton.setOnClickListener {
val intent = Intent(this@YourFragmentName.requireContext(), YourActivityName::class.java)
startActivity(intent)
}
Replace YourFragmentName
with the name of your fragment and YourActivityName
with the name of your activity.
0
for one fragment use: getSupportFragmentManager().beginTransaction().remove(yourFragment).commit();

Iupac
- 11
- 2