在我的MainActivity
中,我添加了名爲FragmentA
,FragmentB
和FragmentC
的三個標籤。分段後臺不工作
在FragmentA
選項卡中,我通過列表片段填充聯繫人列表並將此片段添加到backstack。現在,當我點擊任何列表項時,它會在同一個標籤FragmentA
下顯示該聯繫人的詳細片段。當我按下後退按鈕時,它會返回到聯繫人列表的片段。
關於詳細片段,而不是點擊後退按鈕,當我點擊FragmentB
選項卡時,它應該顯示一個簡單的文本,它工作正常。但是當我點擊返回按鈕時,它應該關閉應用程序,但它回到列表片段,但仍然在FragmentB
選項卡內,因此合併了這兩個片段。
在我MainActivity.java
,
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition() == 0){
PM_Fragment pm_Fragment= new PM_Fragment();
FragmentManager manager= getFragmentManager();
FragmentTransaction frgTrans = manager.beginTransaction();
frgTrans.replace(android.R.id.content, pm_Fragment);
frgTrans.commit();
}
else if(tab.getPosition() == 1){
TabFragmentTwo tabFragmentTwo= new TabFragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("Fragment", "2nd Fragment");
tabFragmentTwo.setArguments(bundle);
FragmentManager manager= getFragmentManager();
FragmentTransaction frgTrans = manager.beginTransaction();
frgTrans.replace(android.R.id.content, tabFragmentTwo);
frgTrans.commit();
}
else {
TabFragmentThree tabFragmentThree= new TabFragmentThree();
Bundle bundle = new Bundle();
bundle.putString("Fragment", "3rd Fragment");
tabFragmentThree.setArguments(bundle);
FragmentManager manager= getFragmentManager();
FragmentTransaction frgTrans = manager.beginTransaction();
frgTrans.replace(android.R.id.content, tabFragmentThree);
frgTrans.commit();
}
}
PM_Fragment.java
顯示的聯繫人列表。
package com.android.myactiobartabs;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class PM_Fragment extends ListFragment
implements OnQueryTextListener , LoaderManager.LoaderCallbacks<Cursor>{
String mCurFilter;
SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ActionBar actionBar = getActivity().getActionBar();
// actionBar.setDisplayHomeAsUpEnabled(false);
MenuButtonUtil.enableMenuButton(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_list_view_with_simple_adapter, container,false);
mAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.pm_fragment, null,
new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { R.id.textView1, R.id.textView2 }, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.pm_fragment_actionitems, menu);
MenuItem searchItem = menu.findItem(R.id.pm_action_search);
SearchView sv = new SearchView(getActivity());
// Changing the color of Searchview widget text field to white.
int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) sv.findViewById(searchSrcTextId);
searchEditText.setTextColor(Color.WHITE);
sv.setOnQueryTextListener(this);
searchItem.setActionView(sv);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.pm_action_delete:
Toast.makeText(getActivity(),"Text_DELETE!",Toast.LENGTH_SHORT).show();
return true;
case R.id.pm_action_edit:
Toast.makeText(getActivity(),"Text_EDIT!",Toast.LENGTH_SHORT).show();
return true;
case R.id.pm_action_add_person:
final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
startActivity(intent);
return true;
case R.id.pm_action_refresh:
Toast.makeText(getActivity(),"Text_REFRESH!",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
getListView().setItemChecked(position, true);
DetailsFragment detailsFragment = new DetailsFragment();
Cursor mycursor = (Cursor) getListView().getItemAtPosition(position);
System.out.println("mycursor.getString(1) " + mycursor.getString(2) +" ");
Bundle bundle = new Bundle();
bundle.putString("CONTACT_NAME", mycursor.getString(1));
bundle.putString("CONTACT_NUMBER", mycursor.getString(2));
detailsFragment.setArguments(bundle);
FragmentManager manager= getActivity().getFragmentManager();
FragmentTransaction frgTrans = manager.beginTransaction();
frgTrans.replace(android.R.id.content, detailsFragment);
frgTrans.addToBackStack("pm_fragment");
frgTrans.commit();
}
@Override
public boolean onQueryTextChange(String newText) {
mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
getLoaderManager().restartLoader(0, null, this);
return true;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
System.out.println("The value of Filter is : "+mCurFilter);
// Run query
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
}
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
};
String selection = "((" +
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " NOTNULL) AND (" +
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " != ''))";
String[] selectionArgs = null;
String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return new CursorLoader(getActivity(), baseUri, projection, selection, selectionArgs, sortOrder);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
mAdapter.swapCursor(null);
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
}
DetailsFragment.java顯示點擊列表項的細節:
package com.android.myactiobartabs;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class DetailsFragment extends Fragment {
TextView textDetail1;
TextView textDetail2;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
//Get ActionBar and navigate UP
ActionBar actionBar = getActivity().getActionBar();
// MenuButtonUtil.enableMenuButton(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_fragment, container, false);
textDetail1 = (TextView)view.findViewById(R.id.textDetail1);
textDetail2 = (TextView)view.findViewById(R.id.textDetail2);
Bundle bundle = getArguments();
if(bundle != null){
String name = bundle.getString("CONTACT_NAME");
String number = bundle.getString("CONTACT_NUMBER");
textDetail1.setText(""+number);
textDetail2.setText(""+name);
}
// getFragmentManager().popBackStack();
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.details_fragment_actionitems, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/*case android.R.id.home:
FragmentManager manager= getActivity().getFragmentManager();
manager.popBackStack("pm_fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction frgTrans = manager.beginTransaction();
frgTrans.commit();
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
getActivity().getActionBar().setHomeButtonEnabled(false);
return true;
*/
case R.id.dt_action_search:
Toast.makeText(getActivity(),"Text_SEARCH!",Toast.LENGTH_SHORT).show();
return true;
case R.id.dt_action_delete:
Toast.makeText(getActivity(),"Text_DELETE!",Toast.LENGTH_SHORT).show();
return true;
case R.id.dt_action_edit:
Toast.makeText(getActivity(),"Text_EDIT!",Toast.LENGTH_SHORT).show();
return true;
case R.id.dt_action_add_person:
final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
TagFragmentTwo.java
顯示上點擊FragmentB
標籤簡單的txt。
package com.android.myactiobartabs;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TabFragmentTwo extends Fragment{
TextView textDetail;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ActionBar actionBar = getActivity().getActionBar();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment, container, false);
textDetail = (TextView)view.findViewById(R.id.simpleTxt);
Bundle bundle = getArguments();
if(bundle != null){
String simpleTxt = bundle.getString("Fragment");
textDetail.setText(""+simpleTxt);
}
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
沒有它不是,男處理應用程序與片段不活動。在單擊每個選項卡時,它會使用「MainActivity」的用戶界面打開一個新的片段,並且沒有任何內容像fragments的「onBackPressed」。 –
顯示從fragmentA切換到fragmentB的代碼。我建議您在單擊2DFragment或3D Fragment時清除片段的後臺堆棧。在該點擊事件時,首先彈出碎片的後臺堆棧,然後加載新的碎片。它肯定會起作用。 – GrIsHu
是的,那就是我正在等待的...... Thnx @GrlsHu,只是掛了一分鐘,包括我在我的帖子中的代碼。 –