Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FragmentHolderActivity
package com.bizzby.ui.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.WindowManager;
import com.bizzby.ui.fragments.BaseIncludeFragment;
import com.bizzby.utils.QLog;
/**
* Created with Intellij with Android, BIZZBY product.
* See licencing for usage of this code.
* <p/>
* User: chris
* Date: 30/01/2013
* Time: 09:59
*/
public class FragmentHolderActivity extends BaseActivity
{
protected static final String EXTRA_FRAGMENT_CLASS = "extra_fragment_class_name";
protected static final String EXTRA_FRAGMENT_BUNDLE = "extra_fragment_bundle";
protected static final String EXTRA_THEME_ID = "extra_fragment_theme_id";
protected static final String EXTRA_SOFTINPUTMODE = "extra_fragment_soft_input_mode";
/**
* Creates a FragmentHolder activity intent
*
* @param activity used to create the intent we are coming from
* @param fragmentClazz The fragment class we want to inflate, remember this must extend {@link com.bizzby.ui.fragments.BaseIncludeFragment}
* @param bundle Optional bundle for the Fragment
* @return
*/
public static final <T extends BaseIncludeFragment> Intent createIntent(final Activity activity, final Class<T> fragmentClazz, final Bundle bundle)
{
final Intent i = new Intent(activity, FragmentHolderActivity.class);
i.putExtra(EXTRA_FRAGMENT_CLASS, fragmentClazz);
i.putExtra(EXTRA_FRAGMENT_BUNDLE, bundle);
return i;
}
/**
* Add the Theme Resource Qualifier to the new Intent.
* <p/>
* If you pass in null, it will pass out null again.
*
* @param intent current new intent from {@link #createIntent(android.app.Activity, Class, android.os.Bundle)}
* @param themeResId Theme id from R.styles.
* @return the passed in Theme with the extra added to it.
*/
public static final Intent addTheme(final Intent intent, final int themeResId)
{
if (intent == null) return null;
intent.putExtra(EXTRA_THEME_ID, themeResId);
return intent;
}
/**
* Set the softinputmode for the window on activity start, by default its hidden so you will need to override it if
* you want to show it on activity start.
*
* @param intent fragment holder intent to attach mode too. If null it will do nothing
* @param windowSoftInputMode get from {@link android.view.WindowManager.LayoutParams} softInputModes, Remember these are a mask so you may need
* to pass a STATE and ADJUST
*/
public static final void addWindowSoftInputMode(final Intent intent, final int windowSoftInputMode)
{
if (intent == null) return;
intent.putExtra(EXTRA_SOFTINPUTMODE, windowSoftInputMode);
}
private Class<BaseIncludeFragment> mIncludeFragmentClass;
private Bundle mIncludeBundle;
public void onCreate(Bundle savedInstanceState)
{
final int themeRes = getIntent().getIntExtra(EXTRA_THEME_ID, 0);
final int softInputMode = getIntent().getIntExtra(
EXTRA_SOFTINPUTMODE,
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
if (themeRes != 0) setTheme(themeRes);
getWindow().setSoftInputMode(softInputMode);
super.onCreate(savedInstanceState);
getExtras(savedInstanceState);
if (null == isFragmentAttached(android.R.id.content))
{
attachIncludeFragment(mIncludeFragmentClass, mIncludeBundle);
}
}
private <T extends BaseIncludeFragment> void attachIncludeFragment(final Class<T> clazz, final Bundle args)
{
if (null == clazz)
throw new IllegalStateException("You must provide a Fragment to attach to this activity");
try
{
final T frag = clazz.newInstance();
if (args != null)
{
frag.setArguments(args);
}
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.disallowAddToBackStack();
ft.replace(android.R.id.content, frag);
ft.commit();
}
catch (InstantiationException e)
{
e.printStackTrace();
QLog.w("Your Include Fragment needs a public constructor");
}
catch (IllegalAccessException e)
{
e.printStackTrace();
QLog.w("Your Include Fragment needs a public constructor");
}
}
private void getExtras(final Bundle bundle)
{
mIncludeFragmentClass = (Class<BaseIncludeFragment>) getIntent().getSerializableExtra(EXTRA_FRAGMENT_CLASS);
mIncludeBundle = getIntent().getBundleExtra(EXTRA_FRAGMENT_BUNDLE);
}
}
@chrisjenx

This comment has been minimized.

Copy link
Owner Author

commented Feb 21, 2013

Added SoftInputMode setter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.