fragment

android jetpack Navigation 获取当前激活的fragment

故事扮演 提交于 2019-12-18 05:17:33
android jetpack Navigation 获取当前激活的fragment 方法一 Fragment mMainNavFragment = getSupportFragmentManager().findFragmentById(R.id.main_nav_fragment); //获取指定的fragment Fragment fragment = mMainNavFragment.getChildFragmentManager().getPrimaryNavigationFragment(); if (fragment instanceof MediaDetailsFragment) { return (MediaDetailsFragment) fragment; } 方法二 Fragment mMainNavFragment = getSupportFragmentManager().findFragmentById(R.id.main_nav_fragment); NavController navController = NavHostFragment.findNavController(mMainNavFragment); int currentFragmentId = navController.getCurrentDestination().getId()

Calling a method in one fragment from another

走远了吗. 提交于 2019-12-18 05:02:42
问题 I've been trying for a while already (as well, searching in here) for something to help me to refresh listView in my MainFragment, when a button is pressed in other fragment. Could sommeone explain me how to make it work? Here's MainFragment with the function (I excluded other functions, since I don't see how they contribute): public class MainFragment extends Fragment { public void otherList() { SQLite db = new SQLite(getActivity()); db.open(); Calendar sCalendar = Calendar.getInstance();

Android: Adding a fragment to an activity

元气小坏坏 提交于 2019-12-18 03:38:22
问题 I'm fairly new to Android, so this might have an obvious answer, but I can't seem to find it. I'm writing a version of a calculator app. I want to make it so that when a user clicks on one of the buttons, this starts a fragment (with more buttons on it that they can then use for more input). The fragment code is as follows: import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class

why we can call getActivity() in onCreateView which run before onActivityCreated?

梦想的初衷 提交于 2019-12-17 21:03:52
问题 I really get confused with Fragment lifecycle, especially for the time to call getActivity() . Sometimes you cannot get Activity by getActivity() . And it always caused some puzzling bugs. Thank you for anyone can solve the puzzle. 回答1: getActivity() can be null while your fragment is in process of preparation and about to be ready. The fragment life cycle is bound to callback methods. These method will be called somewhere in time while fragment is preparing. Fragment.onActivityCreated(Bundle

getChildFragmentManager () cannot be resolved or cannot be referenced

﹥>﹥吖頭↗ 提交于 2019-12-17 19:43:44
问题 I know, there are many forum posts on this topic already, but none did solve my issue My code looks like this: private SectionsPagerAdapter myAdapt; private ViewPager myPager; .... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_screen); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Create the adapter that will return a fragment for each of the three // primary

MapView in Fragment ( Android 4.0 or higher)

天涯浪子 提交于 2019-12-17 19:28:30
问题 I've been looking for a while to find a good tutorial with code example for a MapView in Fragment for ICS. Anyone have any links? 回答1: Here is a book's sample application showing how to have a MapView in a Fragment in an API Level 11+ app. It's mostly just a MapActivity . Here are the key bits of the fragment that loads the MapView : public class MapFragment extends Fragment { private MapView map=null; private MyLocationOverlay me=null; @Override public View onCreateView(LayoutInflater

onCreateOptionsMenu not being called on FragmentActivity when run on phone version

我们两清 提交于 2019-12-17 18:37:39
问题 I create an app that supports both phone and tablet version so i use the android-support-v4.jar library. My activity extends the FragmentActivity and override the onCreateOptionsMenu(Menu menu). This works fine on tablet, the onCreateOptionsMenu being called correctly but it doesn't work on phone, onCreateOptionsMenu never get called. How to resolve this? Note: i use <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="12"/> on Manifest file. 回答1: You should consider from your

Fragment activity catch onKeyDown and use in fragment

孤街醉人 提交于 2019-12-17 18:34:21
问题 I have Fragment activity with pager: List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this, PastEventListFragment.class.getName(),bundle)); fragments.add(Fragment.instantiate(this, EventListFragment.class.getName(),bundle)); this.mPagerAdapter = new EventPagerAdapter(super.getSupportFragmentManager(), fragments); // ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager1); pager.setAdapter(this.mPagerAdapter); pager.setCurrentItem(1); I catch

how to get a fragment added in an XML layout

时光怂恿深爱的人放手 提交于 2019-12-17 18:33:51
问题 I have a layout which includes a fragment as follows: <fragment android:id="@+id/mainImagesList" android:name="com.guc.project.ImagesList" android:layout_width="match_parent" android:layout_height="62dp" android:layout_below="@+id/addimagebutton" android:layout_weight="1" android:paddingTop="55dp" /> now, I need to get this fragment and cast it so I can manipulate it and the updates appear. How can i do so ?! EDIT: I think I've managed to get the fragment, but when I change some variables,

How to open navigation drawer on button click in main fragment?

核能气质少年 提交于 2019-12-17 17:52:07
问题 I have made an app with one activity which uses a navigation drawer to open a number of different fragments. I have the actionbar drawertoggle, but it is not very visible. If I place a button in the onCreateView in my main fragment(the fragment that appears when my app first starts up), how can I get it to open the navigation drawer controlled by my activity? This seems to work. The answer is much simpler than I thought it would be. @Override public View onCreateView(LayoutInflater inflater,