Java Code Examples for androidx.fragment.app.FragmentManager#getFragment()

The following examples show how to use androidx.fragment.app.FragmentManager#getFragment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From LiTr with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();
    if (savedInstanceState != null) {
        mainFragment = fragmentManager.getFragment(savedInstanceState, MAIN_FRAGMENT);
    } else {
        mainFragment = new MainFragment();
    }
    fragmentManager.beginTransaction().replace(R.id.fragment_container, mainFragment).commit();

    if (!hasWriteExternalStoragePermission()) {
        ActivityCompat.requestPermissions(MainActivity.this,
                                          new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE },
                                          REQUEST_STORAGE_PERMISSION);
    }
}
 
Example 2
Source File: Records.java    From FragmentMaster with Apache License 2.0 6 votes vote down vote up
public void restore(FragmentManager fragmentManager, Bundle fragments) {
    mFragments.clear();
    if (fragments != null) {
        Iterable<String> keys = fragments.keySet();
        for (String key : keys) {
            if (key.startsWith("f")) {
                int index = Integer.parseInt(key.substring(1));
                IMasterFragment f = (IMasterFragment) fragmentManager
                        .getFragment(fragments, key);
                if (f != null) {
                    while (mFragments.size() <= index) {
                        mFragments.add(null);
                    }
                    f.setMenuVisibility(false);
                    mFragments.set(index, f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}
 
Example 3
Source File: MainActivity.java    From SkyTube with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// To enable downloading with https on pre-kitkat devices.
	if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
		TLSSocketFactory.setAsDefault();
	}

	// check for updates (one time only)
	if (!updatesCheckerTaskRan) {
		new UpdatesCheckerTask(this, false).executeInParallel();
		updatesCheckerTaskRan = true;
	}

	SkyTubeApp.setFeedUpdateInterval();
	// Delete any missing downloaded videos
	new DownloadedVideosDb.RemoveMissingVideosTask().executeInParallel();

	setContentView(R.layout.activity_main);

	// The Extra variant needs to initialize some Fragments that are used for Chromecast control. This is done in onLayoutSet of BaseActivity.
	// The OSS variant has a no-op version of this method, since it doesn't need to do anything else here.
	onLayoutSet();

	ButterKnife.bind(this);

	if(fragmentContainer != null) {
		if(savedInstanceState != null) {
			final FragmentManager supportFragmentManager = getSupportFragmentManager();
			mainFragment = (MainFragment) supportFragmentManager.getFragment(savedInstanceState, MAIN_FRAGMENT);
			searchVideoGridFragment = (SearchVideoGridFragment) supportFragmentManager.getFragment(savedInstanceState, SEARCH_FRAGMENT);
			channelBrowserFragment = (ChannelBrowserFragment) supportFragmentManager.getFragment(savedInstanceState, CHANNEL_BROWSER_FRAGMENT);
			playlistVideosFragment = (PlaylistVideosFragment) supportFragmentManager.getFragment(savedInstanceState, PLAYLIST_VIDEOS_FRAGMENT);
		}
		handleIntent(getIntent());

	}

	if (savedInstanceState != null) {
		// restore the video blocker plugin
		this.videoBlockerPlugin = (VideoBlockerPlugin) savedInstanceState.getSerializable(VIDEO_BLOCKER_PLUGIN);
		this.videoBlockerPlugin.setActivity(this);
	} else {
		this.videoBlockerPlugin = new VideoBlockerPlugin(this);
	}
}
 
Example 4
Source File: AppcompatActivity.java    From ui with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_actionbar);

	//turn on up button
	getSupportActionBar().setDisplayHomeAsUpEnabled(true);

	
	FragmentManager fragmentManager = getSupportFragmentManager();
	if (savedInstanceState != null) {
		one = (PageFragment) fragmentManager.getFragment(savedInstanceState, "ONE");
		two = (PageFragment) fragmentManager.getFragment(savedInstanceState, "TWO");
		three = (PageFragment) fragmentManager.getFragment(savedInstanceState, "THREE");
		four = (PageFragment) fragmentManager.getFragment(savedInstanceState, "FOUR");
		five = (PageFragment) fragmentManager.getFragment(savedInstanceState, "FIVE");
		//since survived, need to clean up or I can't add them to the pagers adapter again.
		FragmentTransaction remove = fragmentManager.beginTransaction();
		remove.remove(one);
		remove.remove(two);
		remove.remove(three);
		remove.remove(four);
		remove.remove(five);
		if (!remove.isEmpty()) {
			remove.commit();
			fragmentManager.executePendingTransactions();
		}
	} else {
		one = PageFragment.create(1);
		two = PageFragment.create(2);
		three = PageFragment.create(3);
		four = PageFragment.create(4);
		five = PageFragment.create(5);
	}

	viewPager = (ViewPager) findViewById(R.id.pager);
	mPagerAdapter = new myFragmentPagerAdapter(5);
	viewPager.setAdapter(mPagerAdapter);
	//viewPager.setCurrentItem(2);
	//we need to know when a page has changed, so we can change/fix the next/previous/finish buttons
	viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			supportInvalidateOptionsMenu();
		}
	});
}
 
Example 5
Source File: MainActivity.java    From syncthing-android with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Initializes tab navigation.
 */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ((SyncthingApp) getApplication()).component().inject(this);

    setContentView(R.layout.activity_main);
    mDrawerLayout = findViewById(R.id.drawer_layout);

    FragmentManager fm = getSupportFragmentManager();
    if (savedInstanceState != null) {
        mFolderListFragment = (FolderListFragment) fm.getFragment(
                savedInstanceState, FolderListFragment.class.getName());
        mDeviceListFragment = (DeviceListFragment) fm.getFragment(
                savedInstanceState, DeviceListFragment.class.getName());
        mDrawerFragment = (DrawerFragment) fm.getFragment(
                savedInstanceState, DrawerFragment.class.getName());
    } else {
        mFolderListFragment = new FolderListFragment();
        mDeviceListFragment = new DeviceListFragment();
        mDrawerFragment = new DrawerFragment();
    }

    mViewPager = findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = findViewById(R.id.tabContainer);
    tabLayout.setupWithViewPager(mViewPager);
    if (savedInstanceState != null) {
        mViewPager.setCurrentItem(savedInstanceState.getInt("currentTab"));
        if (savedInstanceState.getBoolean(IS_SHOWING_RESTART_DIALOG)){
            showRestartDialog();
        }
        mBatteryOptimizationDialogDismissed = savedInstanceState.getBoolean(BATTERY_DIALOG_DISMISSED);
        if(savedInstanceState.getBoolean(IS_QRCODE_DIALOG_DISPLAYED)) {
            showQrCodeDialog(savedInstanceState.getString(DEVICEID_KEY), savedInstanceState.getParcelable(QRCODE_BITMAP_KEY));
        }
    }

    fm.beginTransaction().replace(R.id.drawer, mDrawerFragment).commit();
    mDrawerToggle = new Toggle(this, mDrawerLayout);
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    setOptimalDrawerWidth(findViewById(R.id.drawer));

    // SyncthingService needs to be started from this activity as the user
    // can directly launch this activity from the recent activity switcher.
    Intent serviceIntent = new Intent(this, SyncthingService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(serviceIntent);
    } else {
        startService(serviceIntent);
    }

    onNewIntent(getIntent());
}