Java Code Examples for android.support.v4.app.FragmentManager#getFragment()

The following examples show how to use android.support.v4.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: ContainerActivity.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_container);
    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = null;
    if (savedInstanceState != null) {
        fragment = fm.getFragment(savedInstanceState, FRAGMENT_TAG);
    }
    if (fragment == null) {
        fragment = initFromIntent(getIntent());
    }
    FragmentTransaction trans = getSupportFragmentManager()
            .beginTransaction();
    trans.replace(R.id.content, fragment);
    trans.commitAllowingStateLoss();
    mFragment = new WeakReference<>(fragment);
}
 
Example 2
Source File: TimelineFragment.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState!=null){
        FragmentManager fragmentManager = getChildFragmentManager();
        articlesFragment = (ArticlesFragment) fragmentManager.getFragment(savedInstanceState, ArticlesFragment.class.getSimpleName());
        favoritesFragment = (FavoritesFragment) fragmentManager.getFragment(savedInstanceState, FavoritesFragment.class.getSimpleName());
        readLaterFragment = (ReadLaterFragment) fragmentManager.getFragment(savedInstanceState, ReadLaterFragment.class.getSimpleName());
    }else {
        articlesFragment = ArticlesFragment.newInstance();
        favoritesFragment = FavoritesFragment.newInstance();
        readLaterFragment = ReadLaterFragment.newInstance();
    }

    new ArticlesPresenter(articlesFragment,
            ArticlesDataRepository.getInstance(ArticlesDataRemoteSource.getInstance()),
            BannerDataRepository.getInstance(BannerDataRemoteSource.getInstance()),
            LoginDataRepository.getInstance(LoginDataLocalSource.getInstance(), LoginDataRemoteSource.getInstance()));

    new FavoritesPresenter(favoritesFragment,
            FavoriteArticlesDataRepository.getInstance(FavoriteArticlesDataRemoteSource.getInstance(),
                    FavoriteArticlesDataLocalSource.getInstance()));

    new ReadLaterPresenter(readLaterFragment,
            ReadLaterArticlesDataRepository.getInstance(ReadLaterArticlesLocalSource.getInstance()));
}
 
Example 3
Source File: MainActivity.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
private void restoreFragments(Bundle savedInstanceState) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    if (savedInstanceState != null) {
        profileFragment = (ProfileFragment)manager.getFragment(savedInstanceState, ProfileFragment.TAG);
        settingsFragment = (SettingsFragment)manager.getFragment(savedInstanceState, SettingsFragment.TAG);
    }

    if (profileFragment == null) {
        profileFragment = new ProfileFragment();
        transaction.add(R.id.fragmentContainer, profileFragment, ProfileFragment.TAG);
    }

    if (settingsFragment == null) {
        settingsFragment = new SettingsFragment();
        transaction.add(R.id.fragmentContainer, settingsFragment, SettingsFragment.TAG);
    }

    transaction.commit();
}
 
Example 4
Source File: LoginActivity.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.container);
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("navigation_bar_tint", true)) {
        getWindow().setNavigationBarColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
    }
    if (savedInstanceState != null) {
        FragmentManager manager = getSupportFragmentManager();
        loginFragment = (LoginFragment) manager.getFragment(savedInstanceState, LoginFragment.class.getSimpleName());
        signUpFragment = (SignUpFragment) manager.getFragment(savedInstanceState, LoginFragment.class.getSimpleName());
    }else {
        loginFragment = LoginFragment.newInstance();
        signUpFragment = SignUpFragment.newInstance();
    }

    if (!loginFragment.isAdded()){
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.view_pager, loginFragment, LoginFragment.class.getSimpleName())
                    .commit();
        }
        if (!signUpFragment.isAdded()){
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.view_pager, signUpFragment, SignUpFragment.class.getSimpleName())
                    .commit();
        }
        new LoginPresenter(loginFragment, LoginDataRepository.getInstance(
                LoginDataLocalSource.getInstance(),
                LoginDataRemoteSource.getInstance()
        ), ReadLaterArticlesDataRepository.getInstance(ReadLaterArticlesLocalSource.getInstance()));
        new LoginPresenter(signUpFragment, LoginDataRepository.getInstance(
                LoginDataLocalSource.getInstance(),
                LoginDataRemoteSource.getInstance()
        ), ReadLaterArticlesDataRepository.getInstance(ReadLaterArticlesLocalSource.getInstance()));
        showLoginFragment();

}
 
Example 5
Source File: MainActivity.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
private void initFragments(Bundle savedInstanceState) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (savedInstanceState != null) {
        mTimelineFragment = (TimelineFragment) fragmentManager.getFragment(savedInstanceState, TimelineFragment.class.getSimpleName());
        mCategoriesFragment = (CategoriesFragment) fragmentManager.getFragment(savedInstanceState, CategoriesFragment.class.getSimpleName());
        mAboutFragment = (AboutFragment) fragmentManager.getFragment(savedInstanceState, AboutFragment.class.getSimpleName());
    } else {
        mTimelineFragment = TimelineFragment.newInstance();
        mCategoriesFragment = CategoriesFragment.newInstance();
        mAboutFragment = AboutFragment.newInstance();
    }
    if (!mTimelineFragment.isAdded()) {
        fragmentManager.beginTransaction()
                .add(R.id.frame_layout, mTimelineFragment, TimelineFragment.class.getSimpleName())
                .commit();
    }
    if (!mCategoriesFragment.isAdded()) {
        fragmentManager.beginTransaction()
                .add(R.id.frame_layout, mCategoriesFragment, CategoriesFragment.class.getSimpleName())
                .commit();
    }

    if (!mAboutFragment.isAdded()) {
        fragmentManager.beginTransaction()
                .add(R.id.frame_layout, mAboutFragment, AboutFragment.class.getSimpleName())
                .commit();
    }
}
 
Example 6
Source File: ActionbarActivity.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_main);

	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.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
		@SuppressLint("NewApi")
		@Override
		public void onPageSelected(int position) {
			// When changing pages, reset the action bar actions since they are dependent
			// on which page is currently active. An alternative approach is to have each
			// fragment expose actions itself (rather than the activity exposing actions),
			// but for simplicity, the activity provides the actions in this sample.
			if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {  //should kept this demo from force closing if run on the wrong API... I think...
				//Note, if below HONEYCOMB, then no buttons, because no actionbar.
				invalidateOptionsMenu();}
		}
	});
}
 
Example 7
Source File: MainActivity.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_main);
	
    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);
	if (viewPager != null) {
	  //in portrait mode
	  viewPager.setAdapter(new ThreeFragmentPagerAdapter(5));
	} else {
		//in landscape mode
		viewPager1 = (ViewPager) findViewById(R.id.pagerleft);
		viewPager1.setAdapter(new ThreeFragmentPagerAdapter(2));
		viewPager2 = (ViewPager) findViewById(R.id.pagerright);
		viewPager2.setAdapter(new ThreeFragmentPagerAdapter(3));
		
	}
	
}
 
Example 8
Source File: MainActivity.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_main);
	//getSupportFragmentManager().beginTransaction().add(R.id.frag_container, firstFragment).commit();
	
	//first find the 3 fragments, if create them as needed.
	//Remember fragments can survive the restart of an activity on orientation change.
	FragmentManager fragmentManager = getSupportFragmentManager();
	if (savedInstanceState != null) {
		leftfrag = (FragLeft) fragmentManager.getFragment(savedInstanceState, "LEFT");
		midfrag  = (FragMid) fragmentManager.getFragment(savedInstanceState,"MIDDLE");
		rightfrag = (FragRight) fragmentManager.getFragment(savedInstanceState,"RIGHT");
		//since survived, remove them from the fragment manager, so don't double add them.
	    FragmentTransaction remove = fragmentManager.beginTransaction();
	    remove.remove(leftfrag);
	    remove.remove(midfrag);
	    remove.remove(rightfrag);
	    if (!remove.isEmpty()) {
	        remove.commit();
	        fragmentManager.executePendingTransactions();
	    }
	} else {
    	leftfrag = new FragLeft();
    	midfrag = new FragMid();
    	rightfrag = new FragRight();
    }

	viewPager = (ViewPager) findViewById(R.id.pager);
	if (viewPager != null) {
	  //in portrait mode
	  viewPager.setAdapter(new ThreeFragmentPagerAdapter(fragmentManager));
	} else {
		//in landscape mode
	      fragmentManager.beginTransaction()
          .add(R.id.frag_left, leftfrag, "LEFT")
          .add(R.id.frag_mid, midfrag, "MIDDLE")
          .add(R.id.frag_right, rightfrag, "RIGHT")
          .commit();
	}
}
 
Example 9
Source File: ActionbarActivity.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.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			supportInvalidateOptionsMenu();
		}
	});
}