Android 片段到活动Backstack返回空白视图

Android 片段到活动Backstack返回空白视图,android,android-layout,android-fragments,android-activity,Android,Android Layout,Android Fragments,Android Activity,我创建了一个家庭活动,其中包括Tablayout和导航抽屉onclick with fragment。我包含了fragmentTransaction.addToBackStack(null.commit()使用片段事务代码返回到上一个活动 我所需的需求来自Activity with tablayout-->NavigationDrawer-->Fragment1-->On BackButtonPress-->main Activity with tablayout. 但是,现在我可以移动到Fra

我创建了一个家庭活动,其中包括
Tablayout和导航抽屉
onclick with fragment。我包含了
fragmentTransaction.addToBackStack(null.commit()使用片段事务代码返回到上一个活动

我所需的需求来自
Activity with tablayout-->NavigationDrawer-->Fragment1-->On BackButtonPress-->main Activity with tablayout.

但是,现在我可以移动到Fragment1,当返回到MainActivity时,视图变成白色的
(如果我使用mainLayout.removeAllViews()
;如果我不使用
mainLayout.removeAllViews();,则片段与MainActivity重叠

1.我的主要活动

2.碎片

3.当我返回主活动时

现在我在我的主要活动中看不到表格

谁能帮帮我吗。

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>
<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>
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));
    }
}

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);
}

鲁宾·内利库纳图。我也有同样的问题。替换
FrameLayout
中的
NavigationView
片段,而不是
RelativeLayout
。因此,您总是要加载
表格布局
查看页面
。 将其放入
mainactivity\u layout.xml
app\u bar\u home.xml
(我看不到您的完整代码,所以您可以尝试这些组合)

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>
<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.addToBackStack(null.commit()fragmentTransaction.commit()进行代码>操作如ρѕρєK所建议。

如我在评论中所建议的,为了找出显示片段的问题,您可以尝试查看正在添加到后堆栈中的片段类型,如下所示
Log.d(“碎片列表”,getSupportFragmentManager().getFragments().toString(); 您可以将其放入
onBackPressed()
onNavigationItemSelected()

根据你的反馈,这对你很有帮助,你解决了问题

还有一些布局设计问题: 您当前的方法是将片段添加到相对布局中。我相信这会导致视图出现一些问题。建议的方法是将片段添加到FrameLayout中,如中所述 此外,您可能希望将所有用于片段的内容移动到视图外部 我已经按照docs.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”
我想这只是未编辑的复制粘贴,但也可能导致一些问题


希望您觉得它有用:)

使用
fragmentTransaction.commit()时会发生什么
而不是
fragmentTransaction.addToBackStack(null.commit()
onNavigationItemSelected
中,向我们显示Backback中有哪些片段。您可以使用以下方法查看它们
Log.d(“FragmentList”,getSupportFragmentManager().getFragments().toString())
您可以将其放入
onBackPressed()
onNavigationItemSelected()
尝试使用将片段添加到Framewlayout中。。您当前的方法是将片段添加到相对布局中。我相信这会导致一些观点上的问题。建议的方法是按照文档中的描述将片段添加到FrameLayout中。另外,您可能希望将所有用于片段的内容移到视图之外。请在回复其他内容时将我称为@ProblemSlover。我不会收到关于您的响应的通知。我已重写了您的布局。请查看您在布局中定义的多个活动,如
tools:context=“.MainActivity”
tools:context=“.HomeActivity”
我猜这只是未编辑的复制粘贴,但也可能导致一些问题..您可以尝试替换fragmentTransaction.replace(R.id.mainlayout,fragment);此语句包含fragmentTransaction.add(R.id.mainlayout,fragment);。谢谢