android-fragments

How to detect when back button pressed in fragment android?

随声附和 提交于 2021-01-27 04:06:35
问题 I have project with navigation drawer with fragment, with 5 menu, the problem is when i go to menu 4 and the i press the back button the app closed, but i need the app back to first menu which is all the menu in fragment. This is code for Main Activity (Navigation Drawer) public class MainActivity extends AppCompatActivity{ DrawerLayout mDrawerLayout; NavigationView mNavigationView; FragmentManager mFragmentManager; FragmentTransaction mFragmentTransaction; @Override protected void onCreate

Update to androidx.fragment:fragment:1.3.0-alpha08: registerForActivityResult not allowed after onCreate anymore. How to use after onCreate?

走远了吗. 提交于 2021-01-22 06:01:49
问题 Initial Question (18/05/2020): So with the latest Update from androidx.fragment:fragment:1.3.0- alpha07 to androidx.fragment:fragment:1.3.0- alpha08 I get the error: FragmentXY is attempting to registerForActivityResult after being created. Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()). I used to check permissions in my StartFragment (Single Activity App, in onViewCreated) after showing to the User information about

Update to androidx.fragment:fragment:1.3.0-alpha08: registerForActivityResult not allowed after onCreate anymore. How to use after onCreate?

拈花ヽ惹草 提交于 2021-01-22 06:01:05
问题 Initial Question (18/05/2020): So with the latest Update from androidx.fragment:fragment:1.3.0- alpha07 to androidx.fragment:fragment:1.3.0- alpha08 I get the error: FragmentXY is attempting to registerForActivityResult after being created. Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()). I used to check permissions in my StartFragment (Single Activity App, in onViewCreated) after showing to the User information about

Access view on content_main.xml from a fragment?

橙三吉。 提交于 2021-01-07 01:28:02
问题 In my app I use NavController with Fragments. In some fragments I need to access views on MainActivity layout. I do that in this way: val fab: FloatingActionButton = requireActivity().findViewById(R.id.fab) This works properly as expected. However, one view does not want to be called and the findViewById() returns null. The layout structure is like this. In activity_main.xml <androidx.drawerlayout.widget.DrawerLayout <androidx.coordinatorlayout.widget.CoordinatorLayout ....> <FrameLayout

making a calculator app and want to update history in second fragment with SharePreferences but second Fragment is not updating

左心房为你撑大大i 提交于 2021-01-07 01:26:35
问题 I am making a calculator app and want to update history in second fragment but not able to find a perfect solution for updating ListView in second fragment. I tried many solutions but none of them worked properly. I'm using ViewPager to swipe between calculator and history fragments . I tired bundle class but if I use bundle in OnCreat method of first Fragment(CalculatorFragment) with ViewPager it shows me blank screen after starting an App & if I use Bundle class in Equal Button it crashes

Call requires API level 16 (current min is 14)

不羁的心 提交于 2021-01-04 04:17:31
问题 I have this line in my code: linearLayout.setBackground(drawable); setBackground() shows the following error: Call requires API level 16 (current min is 14) What does this mean? Can I raise my API level? What does a higher/lower API level mean? Does it limit the amount of devices my app can be used on if the API level is higher? 回答1: What does this mean? It means that the setBackground(Drawable) method was added in API Level 16, and older devices do not have it. However, your app's

Is there any way to Detect userInterations in Android fragments?

坚强是说给别人听的谎言 提交于 2021-01-02 05:54:36
问题 Could any one help me out with this situation. I have implemented OnUserInteraction() method for Android Activity it is working fine for me. But I want it for Fragments too.How can i able call OnUserInteraction() or is there any another way to identify userInteraction with the UI . 回答1: @Sunil's answer causes java.lang.StackOverflowError so I corrected it. Below code works smoothly Create a java class in your app named UserInterationListener and put below code there public interface

why fragment have default constructor?

陌路散爱 提交于 2020-12-31 05:27:43
问题 there is default constructor in fragment, i want to know that what it's use and what functionality it provides? and i run the code without it it worked perfectly and i can't find any error in removing it public class SongListFragment extends Fragment { private static final String SONG_IDS = "song_ids"; // TODO: Rename and change types of parameters private int[] songIds; private OnFragmentInteractionListener mListener; public SongListFragment() { // Required empty public constructor } // TODO

Accessing FragmentManager in a custom view class

杀马特。学长 韩版系。学妹 提交于 2020-12-30 08:26:05
问题 In my custom view I have a Java class running a simple game. When the game is finished I'd like to display my DialogFragment , but the getFragmentManager() method seems to be undefined. FragmentManager manager = getFragmentManager(); Finish finish = new Finish(); finish.show(manager, "done"); I've tried getting the manager through a Fragment obj as: Fragment fragment = new Fragment(); FragmentManager manager = fragment.getFragmentManager(); But that returns as null. I know it's a new Fragment

ViewModel cannot be instantiated in an Activity

巧了我就是萌 提交于 2020-12-30 06:19:54
问题 I am trying to instantiate one ViewModel to use across all of my Activity(s). public class LaunchActivity extends Activity { private Controller control; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch); control = ViewModelProviders.of(this).get(Controller.class); } } but I got an error at control = ViewModelProviders.of(this) <-- stating that it cannot resolve of(this) , but based on the example here by