Questions tagged [fragment]

**DO NOT USE**: fragment is an ambiguous tag, used to refer to numerous technologies. Prefer less ambiguous tags. For Android Fragments, use [android-fragments]. For URL fragments, use [url-fragment]

'fragment' Tag Disambiguation

8161 questions
254
votes
17 answers

Receive result from DialogFragment

I am using DialogFragments for a number of things: choosing item from list, entering text. What is the best way to return a value (i.e. a string or an item from a list) back to the calling activity/fragment? Currently I am making the calling…
James Cross
  • 7,799
  • 8
  • 24
  • 30
154
votes
17 answers

On showing dialog I get "Can not perform this action after onSaveInstanceState"

Some users are reporting, if they use the quick action in the notification bar, they are getting a force close. I show a quick action in the notification who calls the "TestDialog" class. In the TestDialog class after pressing the button "snooze", I…
chrisonline
  • 6,949
  • 11
  • 42
  • 62
151
votes
32 answers

Handling back button in Android Navigation Component

I'd like to know how properly handle system back button action using Navigation Controller. In my app I have two fragments (for ex. fragment1 and fragment2) and I have an action in fragment1 with destination to fragment2. Everything works well…
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
100
votes
18 answers

android.content.Context.getPackageName()' on a null object reference

I am working with Fragments which implements an interface. public class SigninFragment extends Fragment implements SigninInterface The interface's method implementation in the fragment class is as follows. @Override public void…
Karthikeyan Ve
  • 2,550
  • 4
  • 22
  • 40
93
votes
5 answers

FragmentContainerView as NavHostFragment

Seems like using the FragmentContainerView doesn't work right out of the box?
Kurt Acosta
  • 2,407
  • 2
  • 14
  • 29
85
votes
9 answers

How to dismiss a DialogFragment when pressing outside the dialog?

I am using a DialogFragment, and while I have successfully set an image to close (i.e. dismiss) the dialog when pressed, I am having a hard time finding the way to dismiss the dialog when the user clicks anywhere outside it, just as it works with…
sole
  • 2,057
  • 2
  • 17
  • 18
84
votes
10 answers

'setHasOptionsMenu(Boolean): Unit' is deprecated. Deprecated in Java

How do I declare a menu inside of Android fragment? The method that I had used previously is now deprecated. Originally: override fun onCreateView(...): View { setHasOptionsMenu(true) } override fun onCreateOptionsMenu(menu: Menu,…
SUR4IDE
  • 1,003
  • 1
  • 5
  • 11
81
votes
8 answers

If I declare a fragment in an XML layout, how do I pass it a Bundle?

I've got an activity that I've replaced with a fragment. The activity took an Intent that had some extra information on what data the activity was supposed to display. Now that my Activity is just a wrapper around a Fragment that does the same…
Plantage
  • 1,463
  • 2
  • 13
  • 8
79
votes
4 answers

Fragment vs. Custom View in Android

The Fragment and Custom View can achieve the similar function, I know that fragment is more re-usable comparing with custom view, any other benefits/enhancements for using Fragment? Is fragment supposed to replace Custom View, or just a enhancement…
Kai
  • 916
  • 1
  • 7
  • 10
75
votes
5 answers

How to access parent Activity View in Fragment

I have an ActionBarActivity and fragment. I am using FragmentPagerAdapter that provides fragment to my app. My question How can I access parent Activity View in Fragment ??
Bot
  • 2,285
  • 3
  • 17
  • 20
75
votes
5 answers

how to get a fragment added in an XML layout

I have a layout which includes a fragment as follows:
ahmed
  • 857
  • 1
  • 7
  • 10
75
votes
14 answers

How to access Activity Variables from a fragment Android

In the Activity I have : public class tabsmain extends Activity{ public static Context appContext; public boolean lf_ch=false; public void onCreate(Bundle savedInstanceState){ I would like to access and possibly change lf_ch from a…
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
70
votes
6 answers

reusing fragments in a fragmentpageradapter

I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new fragment in the getItem method which seems wasteful. Is there a FragmentPagerAdapter equivalent to the convertView in the listAdapter that will enable me…
jwanga
  • 4,166
  • 4
  • 26
  • 27
70
votes
5 answers

Fragment inner class should be static

I have a FragmentActivity class with inner class that should display Dialog. But I am required to make it static. Eclipse offers me to suppress error with @SuppressLint("ValidFragment"). Is it bad style if I do it and what are the possible…
user2176737
  • 789
  • 1
  • 5
  • 10
67
votes
9 answers

How to display AlertDialog in a Fragment?

I want to display an alert dialog in my app. I am using fragments. I tried the below code to do this: AlertDialog ad = new AlertDialog.Builder(context) .create(); ad.setCancelable(false); ad.setTitle(title); …
andro-girl
  • 7,989
  • 22
  • 71
  • 94
1
2 3
99 100