-3

MainActivityFragmentA, FragmentBFragmentC이라는 세 개의 탭을 추가하고 있습니다.조각의 백 스택이 작동하지 않습니다.

FragmentA 탭에서 목록 조각을 통해 연락처 목록을 채우고이 조각을 백 스택에 추가합니다. 이제 목록 항목 중 하나를 클릭하면 해당 연락처의 세부 조각이 동일한 탭 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.javaFragmentB 탭을 클릭에 대한 간단한 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); 
     } 

    } 
+0

없음이 아니, 아니 활동과 조각과 응용 프로그램을 다루는 M.하지 각각의 탭을 클릭하면'MainActivity'의 UI를 사용하여 새로운 조각을 열고, 단편에 대해서는'onBackPressed'와 같은 것이 없습니다. –

+0

fragmentA에서 fragment B로 전환하는 코드를 보여주세요. 2DFragment 또는 3D Fragment를 클릭 할 때마다 조각의 배낭을 지우라고 제안합니다. 해당 click 이벤트에서 먼저 fragment의 backstack을 pop 한 다음 새로운 fragment를로드하십시오. 확실히 작동 할 것입니다. – GrIsHu

+0

그래, 그게 내가 기다리고있는 것 ... Thnx @ GrlsHu, 잠깐만, 내 코드에 내 게시물에 포함. –

답변

1

난 당신이 2DFragment 또는 3D 조각를 클릭 할 때마다 조각의 가기 backstack을 취소를 건의 할 것입니다. 해당 click 이벤트에서 먼저 fragment의 backstack을 pop 한 다음 새로운 fragment를로드하십시오. 확실히 작동 할 것입니다.

새 조각을로드하기 전에 탭을 클릭 할 때 아래 코드를 작성하십시오.

  FragmentManager fm = getSupportFragmentManager(); 
      FragmentTransaction ft = fm.beginTransaction(); 
      fm.popBackStack(); 

편집 : 같은 아래 MainActivity에서

변경 코드 :

else if(tab.getPosition() == 1){ 

    FragmentManager manager= getFragmentManager(); 
    FragmentTransaction frgTrans = manager.beginTransaction(); 
    manager.popBackStack(); 

     TabFragmentTwo tabFragmentTwo= new TabFragmentTwo(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("Fragment", "2nd Fragment"); 

    tabFragmentTwo.setArguments(bundle); 
    frgTrans.replace(android.R.id.content, tabFragmentTwo); 
    frgTrans.commit(); 
} 

else { 
    FragmentManager manager= getFragmentManager(); 
    FragmentTransaction frgTrans = manager.beginTransaction(); 
    manager.popBackStack(); 
    TabFragmentThree tabFragmentThree= new TabFragmentThree(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("Fragment", "3rd Fragment"); 

    tabFragmentThree.setArguments(bundle); 

    frgTrans.replace(android.R.id.content, tabFragmentThree); 
    frgTrans.commit(); 
    } 
+0

OMG ... 그것은 단지 매력처럼 작동했습니다 ... thnx alot ... 나는 다른 사람들이 왜 내 질문에 세 번이나 등급을 매기는지 안다. –