Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在新的片段版本下可以看到以前的片段_Android - Fatal编程技术网

Android 在新的片段版本下可以看到以前的片段

Android 在新的片段版本下可以看到以前的片段,android,Android,我已经创建了带有导航抽屉的android应用程序,当我单击导航抽屉中的项目时,旧片段未被替换,我仍然可以在新片段下看到旧片段。如何解决此问题 private void displaySelectedScreen(int id) { Fragment fragment = null; switch (id) { case R.id.home: fragment = new Main()

我已经创建了带有导航抽屉的android应用程序,当我单击导航抽屉中的项目时,旧片段未被替换,我仍然可以在新片段下看到旧片段。如何解决此问题

private void displaySelectedScreen(int id)
    {
        Fragment fragment = null;

        switch (id)
        {
            case R.id.home:
                fragment = new Main();
                break;
            case R.id.settings:
                Intent intent = new Intent(this, Settings.class);
                startActivity(intent);
                break;
            case R.id.about:
                Intent intent1 = new Intent(this, About.class);
                startActivity(intent1);
                break;
            case R.id.share:
                try
                {
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("text/plain");
                    i.putExtra(Intent.EXTRA_SUBJECT, "Audiophileradio");
                    String sAux = "\nLet me recommend you this application\n\n";
                    sAux = sAux + "https://play.google.com/store/apps/details?id=Orion.Soft \n\n";
                    i.putExtra(Intent.EXTRA_TEXT, sAux);
                    startActivity(Intent.createChooser(i, "choose one"));
                }
                catch(Exception e)
                {
                    //e.toString();
                }
                break;
            case R.id.send:
                fragment = new Feedback();
                break;
        }
        if (fragment != null)
        {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.contentFrame, fragment).commit();
        }

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

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        displaySelectedScreen(id);

        return true;
    }
content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="audiophileradio.example.com.audiophileradio.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:background="@drawable/backg1">

    <FrameLayout
        android:id="@+id/contentFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </FrameLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="13dp"
        android:layout_height="21dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="+"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.977"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.13"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"/>



您可以为片段添加标记

getSupportFragmentManager().beginTransaction()
.add(R.id.frame, new Main()(), "content_fragment")
.commit();
按标签查找

getSupportFragmentManager().findFragmentByTag("content_fragment");
添加或删除

        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag("content_fragment");
        if(fragment == null)
        {
            // Create the detail fragment and add it to the activity using a fragment transaction.
            mainFragment = new Main();
            fragmentManager.beginTransaction()
                    .add(R.id.contentFrame, mainFragment,"content_fragment")
                    .commit();
        }
        else
        {
            // get old fragment
            mainFragment = (Main)fragment;
        }

您可以向片段或contentFrame添加背景:

<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">


屏幕截图会有所帮助。无论如何,你总是可以放置一个不透明的背景,然后你就看不到后面的内容了。我尝试放置黑色背景,但我仍然可以看到旧片段中的内容。你在菜单中同时使用片段和活动-你确定你没有看到活动顶部的活动吗。请添加一个截图。在新的片段中应该只有标题和编辑文本!!!我已经解决了问题,我不想开始活动!我想开始零碎,请检查一下。你有问题吗?你能给我详细解释一下吗?变量片段已经在作用域中定义了,无法解决symbol mainFragmentWhat’s Problem?确切地说,您可以尝试使用不同的颜色“#4f00”,例如,并提供结果的屏幕截图吗?你也可以分享片段的xml吗?我把颜色改成了“#4f00”,但仍然是同一个问题。我在改变背景颜色后为新片段和屏幕截图添加了xml!!!
<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">