forked from organicmaps/organicmaps
[android] Refactored & simplified the way default fragment activity handles fragments.
This commit is contained in:
parent
df35674269
commit
5c390d5aef
6 changed files with 93 additions and 45 deletions
|
@ -1,6 +1,8 @@
|
|||
package com.mapswithme.maps.base;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.MenuItem;
|
||||
|
@ -18,6 +20,9 @@ public class BaseMwmFragmentActivity extends AppCompatActivity
|
|||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
final int layoutId = getContentLayoutResId();
|
||||
if (layoutId != 0)
|
||||
setContentView(layoutId);
|
||||
|
||||
// Use full-screen on Kindle Fire only
|
||||
if (Utils.isAmazonDevice())
|
||||
|
@ -27,6 +32,8 @@ public class BaseMwmFragmentActivity extends AppCompatActivity
|
|||
}
|
||||
|
||||
MWMApplication.get().initStats();
|
||||
|
||||
attachDefaultFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -83,4 +90,56 @@ public class BaseMwmFragmentActivity extends AppCompatActivity
|
|||
{
|
||||
setSupportActionBar(getToolbar());
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to set custom content view.
|
||||
* @return layout resId.
|
||||
*/
|
||||
protected int getContentLayoutResId()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected void attachDefaultFragment()
|
||||
{
|
||||
final String fragmentName = getFragmentClassName();
|
||||
if (fragmentName != null)
|
||||
replaceFragment(getFragmentClassName(), false, getIntent().getExtras());
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace attached fragment with the new one.
|
||||
*/
|
||||
public void replaceFragment(String fragmentClassName, boolean addToBackStack, Bundle args)
|
||||
{
|
||||
final int resId = getFragmentContentResId();
|
||||
if (findViewById(resId) == null)
|
||||
throw new IllegalStateException("Fragment can't be added, since getFragmentContentResId() isn't implemented or returns wrong resourceId.");
|
||||
|
||||
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
final Fragment fragment = Fragment.instantiate(this, fragmentClassName, args);
|
||||
transaction.replace(resId, fragment, fragmentClassName);
|
||||
if (addToBackStack)
|
||||
transaction.addToBackStack(null);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to automatically attach fragment in onCreate. Tag applied to fragment in back stack is set to fragment name, too.
|
||||
* WARNING : if custom layout for activity is set, getFragmentContentResId() must be implemented, too.
|
||||
* @return class name of the fragment, eg FragmentClass.getName()
|
||||
*/
|
||||
protected String getFragmentClassName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get resource id for the fragment. That must be implemented to return correct resource id, if custom layout is set.
|
||||
* @return resourceId for the fragment
|
||||
*/
|
||||
protected int getFragmentContentResId()
|
||||
{
|
||||
return android.R.id.content;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
|
@ -16,15 +14,27 @@ public class BookmarkCategoriesActivity extends BaseMwmFragmentActivity
|
|||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.activity_fragment_and_toolbar);
|
||||
final Toolbar toolbar = getToolbar();
|
||||
toolbar.setTitle(R.string.bookmark_sets);
|
||||
UiUtils.showHomeUpButton(toolbar);
|
||||
displayToolbarAsActionBar();
|
||||
}
|
||||
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
Fragment fragment = Fragment.instantiate(this, BookmarkCategoriesFragment.class.getName(), getIntent().getExtras());
|
||||
transaction.replace(R.id.fragment_container, fragment, "fragment");
|
||||
transaction.commit();
|
||||
@Override
|
||||
protected int getContentLayoutResId()
|
||||
{
|
||||
return R.layout.activity_fragment_and_toolbar;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFragmentClassName()
|
||||
{
|
||||
return BookmarkCategoriesFragment.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getFragmentContentResId()
|
||||
{
|
||||
return R.id.fragment_container;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,6 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
|
@ -16,15 +14,21 @@ public class BookmarkListActivity extends BaseMwmFragmentActivity
|
|||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.activity_fragment_and_toolbar);
|
||||
final Toolbar toolbar = getToolbar();
|
||||
toolbar.setTitle(R.string.bookmarks);
|
||||
UiUtils.showHomeUpButton(toolbar);
|
||||
displayToolbarAsActionBar();
|
||||
}
|
||||
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
Fragment fragment = Fragment.instantiate(this, BookmarksListFragment.class.getName(), getIntent().getExtras());
|
||||
transaction.replace(R.id.fragment_container, fragment, "fragment");
|
||||
transaction.commit();
|
||||
@Override
|
||||
protected int getContentLayoutResId()
|
||||
{
|
||||
return R.layout.activity_fragment_and_toolbar;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getFragmentContentResId()
|
||||
{
|
||||
return R.id.fragment_container;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,6 @@ package com.mapswithme.maps.bookmarks;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
|
||||
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
|
||||
|
||||
|
@ -16,14 +13,9 @@ public class ChooseBookmarkCategoryActivity extends BaseMwmFragmentActivity
|
|||
public static final int REQUEST_CODE_EDIT_BOOKMARK = 0x2;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
protected String getFragmentClassName()
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
Fragment fragment = Fragment.instantiate(this, ChooseBookmarkCategoryFragment.class.getName(), getIntent().getExtras());
|
||||
transaction.replace(android.R.id.content, fragment, "fragment");
|
||||
transaction.commit();
|
||||
return ChooseBookmarkCategoryFragment.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -2,9 +2,6 @@ package com.mapswithme.maps.search;
|
|||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
|
||||
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
|
||||
|
||||
|
@ -13,14 +10,9 @@ public class SearchActivity extends BaseMwmFragmentActivity
|
|||
public static final String EXTRA_QUERY = "search_query";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
protected String getFragmentClassName()
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
Fragment fragment = Fragment.instantiate(this, SearchFragment.class.getName(), getIntent().getExtras());
|
||||
transaction.replace(android.R.id.content, fragment, "fragment");
|
||||
transaction.commit();
|
||||
return SearchFragment.class.getName();
|
||||
}
|
||||
|
||||
public static void startForSearch(Context context, String query)
|
||||
|
|
|
@ -1,21 +1,12 @@
|
|||
package com.mapswithme.maps.settings;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
|
||||
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
|
||||
|
||||
public class StoragePathActivity extends BaseMwmFragmentActivity
|
||||
{
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
protected String getFragmentClassName()
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
Fragment fragment = Fragment.instantiate(this, StoragePathFragment.class.getName(), getIntent().getExtras());
|
||||
transaction.replace(android.R.id.content, fragment, "fragment");
|
||||
transaction.commit();
|
||||
return StoragePathFragment.class.getName();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue