package net.tasksnow.view.slidingmenu;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import net.tasksnow.R;
import net.tasksnow.view.slidingmenu.SlidingMenuListView.OnCategorySelectListener;
import com.luma.android.andrologger.Logger;
import com.slidingmenu.lib.SlidingMenu;
/**
* @author D056943
* @since 10:25:49 - 19.12.2012
* @project cFoldersDemo
*/
public abstract class SlidingMenuActivity extends Activity implements OnCategorySelectListener {
// ===========================================================
// Constants
// ===========================================================
private static final String MENU_OPEN_KEY = "menuopen_key";
// ===========================================================
// Fields
// ===========================================================
protected SlidingMenu menu;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.slidingmenu_activity);
this.menu = (SlidingMenu) LayoutInflater.from(this).inflate(R.layout.slidingmenu, null);
this.menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
this.menu.setMenu(R.layout.menu_list);
this.getSlidingMenuListView().setOnCategorySelectListener(this);
MenuContent menuContent = (MenuContent) this.getLastNonConfigurationInstance();
if (menuContent == null) {
menuContent = this.onGenerateListAdapter();
}
this.getSlidingMenuListView().setMenuContent(menuContent);
this.getSlidingMenuListView().updateListAdapter();
if (this.getActionBar() != null)
this.getActionBar().setDisplayHomeAsUpEnabled(true);
if (this.findViewById(R.id.container_list) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
Fragment firstFragment = this.getFirstFragment();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(this.getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
this.getFragmentManager().beginTransaction().add(R.id.container_list, firstFragment).commit();
}
Logger.onCreate(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
Logger.onDestroy(this);
}
@Override
protected void onResume() {
super.onResume();
Logger.onResume(this);
}
@Override
public Object onRetainNonConfigurationInstance() {
return this.getSlidingMenuListView().getMenuContent();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.getBoolean(MENU_OPEN_KEY)) {
this.menu.showMenu();
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(MENU_OPEN_KEY, this.menu.isMenuShowing());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.menu.toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCategorySelected(Object executeObject) {
if ((executeObject != null) && (executeObject instanceof Fragment)) {
this.replaceContentFragment((Fragment) executeObject);
}
this.getSlidingMenu().toggle();
}
// ===========================================================
// Methods
// ===========================================================
public void replaceContentFragment(Fragment newFragment) {
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.container_list, newFragment);
//TODO Durch entfernen muss die View immer wieder neu aufgebaut werden
// transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
public abstract MenuContent onGenerateListAdapter();
public Fragment getFirstFragment() {
Object firstExecuteObject = this.getMenuContent().getSelectedItem().onExecuteAction();
if (firstExecuteObject instanceof Fragment) {
return (Fragment) firstExecuteObject;
}
return null;
}
// ===========================================================
// Getter & Setter
// ===========================================================
public SlidingMenu getSlidingMenu() {
return this.menu;
}
public MenuContent getMenuContent() {
return this.getSlidingMenuListView().getMenuContent();
}
public SlidingMenuListView getSlidingMenuListView() {
return ((SlidingMenuListView) this.menu.getMenu());
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}