public class MainActivity extends FragmentActivity implements CustomEmpDialogFragment.EmpDialogFragmentListener {
private Fragment contentFragment;
private EmpListFragment employeeListFragment;
private EmpAddFragment employeeAddFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
if (savedInstanceState != null)
{
if (savedInstanceState.containsKey("content"))
{
String content = savedInstanceState.getString("content");
if (content.equals(EmpAddFragment.ARG_ITEM_ID))
{
if (fragmentManager.findFragmentByTag(EmpAddFragment.ARG_ITEM_ID) != null)
{
setFragmentTitle(R.string.add_emp);
contentFragment = fragmentManager.findFragmentByTag(EmpAddFragment.ARG_ITEM_ID);
}
}
}
if (fragmentManager.findFragmentByTag(EmpListFragment.ARG_ITEM_ID) != null)
{
employeeListFragment = (EmpListFragment) fragmentManager.findFragmentByTag(EmpListFragment.ARG_ITEM_ID);
contentFragment = employeeListFragment;
}
}
else
{
employeeListFragment = new EmpListFragment();
setFragmentTitle(R.string.app_name);
switchContent(employeeListFragment, EmpListFragment.ARG_ITEM_ID);
}
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
if (contentFragment instanceof EmpAddFragment)
{
outState.putString("content", EmpAddFragment.ARG_ITEM_ID);
}
else
{
outState.putString("content", EmpListFragment.ARG_ITEM_ID);
}
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_add:
setFragmentTitle(R.string.add_emp);
employeeAddFragment = new EmpAddFragment();
switchContent(employeeAddFragment, EmpAddFragment.ARG_ITEM_ID);
return true;
}
return super.onOptionsItemSelected(item);
}
public void switchContent(Fragment fragment, String tag)
{
FragmentManager fragmentManager = getSupportFragmentManager();
while (fragmentManager.popBackStackImmediate());
if (fragment != null)
{
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment, tag);
if (!(fragment instanceof EmpListFragment))
{
transaction.addToBackStack(tag);
}
transaction.commit();
contentFragment = fragment;
}
}
protected void setFragmentTitle(int resourseId)
{
setTitle(resourseId);
getActionBar().setTitle(resourseId);
}
@Override
public void onBackPressed()
{
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0)
{
super.onBackPressed();
}
else if (contentFragment instanceof EmpListFragment|| fm.getBackStackEntryCount() == 0)
{
onShowQuitDialog();
}
}
public void onShowQuitDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do You Want To Quit?");
builder.setPositiveButton(android.R.string.yes,new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
finish();
}
});
builder.setNegativeButton(android.R.string.no,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
builder.create().show();
}
@Override
public void onFinishDialog()
{
if (employeeListFragment != null)
{
employeeListFragment.updateView();
}
}}
这里我用CRUD操作创建一个数据库, 编译这些代码时发现了一些错误,下面给出了
错误:(35,76)错误:不兼容的类型:android.support.v4.app.Fragment无法转换为android.app.Fragment 错误:(41,91)错误:不兼容的类型:片段无法转换为EmpListFragment 错误:(91,53)错误:不兼容的类型:android.app.Fragment无法转换为android.support.v4.app.Fragment 错误:任务':app:compileDebugJavaWithJavac'执行失败。 编译失败;有关详细信息,请参阅编译器错误输出。