3

我已經創建了一個家庭活動,其中包括Tablayout and Navigation Drawer onclick與片段。我已將fragmentTransaction.addToBackStack(null).commit();與片段事務代碼包括在一起以返回到先前的活動。片段到活動Backstack返回空白視圖

我要的要求是從Activity with tablayout-->NavigationDrawer-->Fragment1--> On BackButtonPress-->MainActivity with tablayout.

不過,現在我已經能夠移動到片段1,而當回到MainActivity,視圖成爲filled with white(if i use mainLayout.removeAllViews();如果不會使用mainLayout.removeAllViews(); ,then the fragment is overlapping with the MainActivity

1.My mainactivty

My Mainactivty

2.fragment

when goes to a fragment

3.當我回到mainactivty

when i returned to mainactivity

現在我不能在我的MainActivity中看到tablayout。

任何人都可以請幫助我。

mainactivity_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".HomeActivity" 
    android:id="@+id/mainlayout" 
    tools:showIn="@layout/app_bar_home"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rel"> 
     <RelativeLayout 
      android:id="@+id/main_layout1" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:context=".MainActivity"> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tab_layout1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="?attr/colorPrimary" 
       android:elevation="6dp" 
       android:minHeight="?attr/actionBarSize" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/pager1" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@id/tab_layout1"/> 

     </RelativeLayout> 
    </RelativeLayout> 



</RelativeLayout> 

MainActivity.java

import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.support.design.widget.NavigationView; 
    import android.support.design.widget.TabLayout; 
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.app.FragmentTransaction; 
    import android.support.v4.view.GravityCompat; 
    import android.support.v4.view.ViewPager; 
    import android.support.v4.widget.DrawerLayout; 
    import android.support.v7.app.ActionBarDrawerToggle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.RelativeLayout; 

    import java.io.File; 
    import java.util.ArrayList; 


    public class HomeActivity extends AppCompatActivity 
      implements NavigationView.OnNavigationItemSelectedListener { 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_home); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
      drawer.setDrawerListener(toggle); 
      toggle.syncState(); 

      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
      navigationView.setNavigationItemSelectedListener(this); 

      TabLayout tabLayout1 = (TabLayout) findViewById(R.id.tab_layout1); 
      tabLayout1.addTab(tabLayout1.newTab().setIcon(R.drawable.tab_ic_home)); 
      tabLayout1.addTab(tabLayout1.newTab().setIcon(R.drawable.tab_ic_map)); 
      tabLayout1.addTab(tabLayout1.newTab().setIcon(R.drawable.tab_ic_login)); 

      tabLayout1.setTabGravity(TabLayout.GRAVITY_FILL); 

      final ViewPager viewPager1 = (ViewPager) findViewById(R.id.pager1); 
      final PagerAdapter1 adapter = new PagerAdapter1 
        (getSupportFragmentManager(), tabLayout1.getTabCount()); 
      viewPager1.setAdapter(adapter); 
      viewPager1.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1)); 
      tabLayout1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
       @Override 
       public void onTabSelected(TabLayout.Tab tab) { 
        viewPager1.setCurrentItem(tab.getPosition()); 
       } 

       @Override 
       public void onTabUnselected(TabLayout.Tab tab) { 

       } 

       @Override 
       public void onTabReselected(TabLayout.Tab tab) { 

       } 
      }); 
     } 

     @Override 
     public void onBackPressed() { 
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      if (drawer.isDrawerOpen(GravityCompat.START)) { 
       drawer.closeDrawer(GravityCompat.START); 
      } else { 
       super.onBackPressed(); 
      } 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.home, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 

     @SuppressWarnings("StatementWithEmptyBody") 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      // Handle navigation view item clicks here. 
      int id = item.getItemId(); 
      RelativeLayout mainLayout=(RelativeLayout)findViewById(R.id.main_content); 
      if (id == R.id.nav_login) { 
       LoginFragment fragment = new LoginFragment(); 
    //   mainLayout.removeAllViews(); 
       fragmentTransaction.replace(R.id.mainlayout, fragment); 
       fragmentTransaction.addToBackStack(null).commit(); 
      } 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 
      return true; 
     } 
    } 

Fragment.java

import android.annotation.SuppressLint; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class ContactFragment extends Fragment implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    Button call; 

    public ContactFragment() { 
     // Required empty public constructor 
    } 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.activity_contact,container,false); 

     call = (Button) v.findViewById(R.id.button5); 
     call.setOnClickListener(new View.OnClickListener() { 
      @SuppressLint("NewApi") 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(Intent.ACTION_DIAL); 
       i.setData(Uri.parse("tel:7034409615")); 
       startActivity(i); 
      } 
     }); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     return v; 


    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(9.2700, 76.7800); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Pathanamthitta")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 
+1

什麼用'fragmentTransaction.commit()時發生;',而不是'fragmentTransaction.addToBackStack(空).commit();''中?? onNavigationItemSelected' –

+0

你必須在addToBackStack(「Login-or any」)中傳遞一些參數; ....當用戶後退時,你已經overBided onBackPressed()與導航抽屜。 –

+0

應用程序會關閉當前的分段屏幕。不會回到mainactivty。 –

回答

3

正如我在評論中建議,爲了找出問題的片段顯示,你可以嘗試查看如下
Log.d(「FragmentList」被添加什麼樣的碎片進入回堆,getSupportFragmentManager ().getFragments()的toString(); 你可以把它裏面onBackPressed()onNavigationItemSelected()

根據您的反饋意見是對你有幫助,你想通了這個問題

也有一些佈局設計問題: 您目前的做法是添加frag進入相對佈局。我認爲這會導致一些觀點上的問題。推薦的方法是在docs 描述您可能還需要移動用於片段 一切外界看來我已經重寫你的佈局下面的文檔建議的準則,以片段加入到FrameLayout裏。 P.S我沒有測試過它是如何顯示的..你可能需要調整一些佈局參數。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
     <!-- 
     Layout for fragments 
     --> 
     <FrameLayout 
        android:id="@+id/mainlayout" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:paddingBottom="@dimen/activity_vertical_margin" 
         app:layout_behavior="@string/appbar_scrolling_view_behavior" 
         tools:showIn="@layout/app_bar_home"> 

     </RelativeLayout> 
     <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/rel"> 
      <RelativeLayout 
        android:id="@+id/main_layout1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

       <android.support.design.widget.TabLayout 
         android:id="@+id/tab_layout1" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="?attr/colorPrimary" 
         android:elevation="6dp" 
         android:minHeight="?attr/actionBarSize" 
         android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:orientation="horizontal"/> 

       <android.support.v4.view.ViewPager 
         android:id="@+id/pager1" 
         android:layout_width="match_parent" 
         android:layout_height="fill_parent" 
         android:layout_below="@id/tab_layout1"/> 

      </RelativeLayout> 
     </RelativeLayout> 
    </LinearLayout> 

而且你在你的佈局像tools:context=".MainActivity"tools:context=".HomeActivity"我想這只是未經編輯複製粘貼定義了多個活動,但可能會導致一些問題,以及。

希望你覺得它有用:)

1

在你MainActivity.java補充一點:

protected OnBackPressedListener onBackPressedListener; 

public interface OnBackPressedListener { 
    void doBack(); 
} 

public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) { 
    this.onBackPressedListener = onBackPressedListener; 
} 

@Override 
public void onBackPressed() { 
    if (onBackPressedListener != null) 
     onBackPressedListener.doBack(); 
    else 
     super.onBackPressed(); 
} 

Fragment.java實現類是這樣的:

implements MainActivity.OnBackPressedListener 

並實現方法:

@Override 
public void doBack() { 
    Intent yourMainActivity = new Intent(getActivity(), MainActivity.class); 
    startActivity(yourMainActivity); 
} 
1

魯賓Nellikunnathu。我遇到過同樣的問題。替換FrameLayout中的NavigationView片段,而不是RelativeLayout。所以你總是會加載TabLayoutViewPager。 將它放在mainactivity_layout.xmlapp_bar_home.xml(我沒有看到你的完整代碼,所以你試試這些組合)。

mainactivity_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".HomeActivity" 
    android:id="@+id/mainlayout" 
    tools:showIn="@layout/app_bar_home"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rel"> 
    <RelativeLayout 
     android:id="@+id/main_layout1" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".MainActivity"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:elevation="6dp" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager1" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@id/tab_layout1"/> 

     </RelativeLayout> 
    </RelativeLayout> 

    // Replace NavigationView fragments in this FrameLayout 
    <FrameLayout 
     android:id="@+id/flForReplace" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".FrameLayout" /> 

</RelativeLayout> 

,你的代碼應該是

fragmentTransaction.replace(R.id.flForReplace, fragment); 

fragmentTransaction.commit();取代fragmentTransaction.addToBackStack(null).commit();作爲ρяσѕρєяķ建議。