最初のフラグメントは、移行後にバックグラウンドで残っています

Svestis:

私は現在、スプラッシュ画面、ログイン画面と登録画面を持つアプリケーションを作成しようとしています。

私が必要とする正確に何を達成するための唯一の方法は、フラグメントを使用することです。私は、ログインにスプラッシュ画面から移行しようとするものの、スプラッシュ画面の遺骨の一部であったテキストビューをフラグメント化。同じレジスタフラグメントにログインフラグメントから移行した後だけでなく、ログインフラグメントにレジスタフラグメント後ろから起こります。そこエディットテキスト滞在その事実を除いて - 何の問題は、ログインの間および登録または登録とログインの間を移行中に存在しないことに注意することが重要です。

俺の MainActivity.java

public class StartActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
    }

    @Override
    public void onBackPressed(){
        Toast.makeText(getApplicationContext(), "Back button is disabled", Toast.LENGTH_SHORT).show();
    }
}

以下のための私のレイアウト MainActivity

<?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"
    tools:context=".StartActivity"
    android:background="@mipmap/background">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerstart"
        android:layout_centerInParent="true"
        android:name="com.example.distributedsystemsui.SplashFragment">

    </fragment>
</RelativeLayout>

ザ・ SplashScreen.java

public class SplashFragment extends Fragment {
    LinearLayout linearLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_splash, container, false);
        linearLayout = (LinearLayout) view.findViewById(R.id.f_linearsplash);
        Animation animation_fadein = AnimationUtils.loadAnimation((StartActivity)getActivity(), R.anim.fade_in);
        linearLayout.startAnimation(animation_fadein);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
                        R.anim.slide_enter_right, R.anim.slide_exit_right);
                LoginFragment loginFragment = LoginFragment.newInstance();
                ft.replace(R.id.viewpagerstart, loginFragment);
                ft.commit();
            }
        }, 3000);
        return view;

    }
}

そして、のレイアウトSplashScreenの断片。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".LoginFragment"
    android:background="@color/Tranparent"
    android:id="@+id/frame_splash">

    <LinearLayout
        android:id="@+id/f_linearsplash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/f_splashlogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="70dp"
            android:background="#FFFFFF"/>

        <TextView
            android:id="@+id/f_splashtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="100dp"
            android:background="#FFFFFF"/>
    </LinearLayout>
</FrameLayout>

すべての私の検索は、バック何につながりました。

私はMainActivity.xmlに含まれる以下のいずれかが(私は変更しようとしましたが、運)混乱を作成することを想定することができます:

android:name="com.example.distributedsystemsui.SplashFragment"

または私はミスをしていますことをSplashScreen.java、私はそれを動作させるために5時間以上試みたにもかかわらず、私は見つけることができません。

ianhanniballake:

問題は、あなたが経由で追加の断片置き換えることができないということである<fragment>タグを。

代わりに、あなたはどちらかのはず。

1)スイッチFragmentContainerViewに加え、フラグメント1.2.0これは、同じ問題に悩まされない<fragment>タグとあなたが行うことができますreplace問題なく操作を:

<?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"
    tools:context=".StartActivity"
    android:background="@mipmap/background">

    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerstart"
        android:layout_centerInParent="true"
        android:name="com.example.distributedsystemsui.SplashFragment">

    </androidx.fragment.app.FragmentContainerView>
</RelativeLayout>

2)を使用しFrameLayoutます。レイアウトで、手動で追加しSplashFragment、これは基本的に何である(あなたの活動にFragmentContainerView)あなたのために行います。

<?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"
    tools:context=".StartActivity"
    android:background="@mipmap/background">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerstart"
        android:layout_centerInParent="true">

    </FrameLayout>
</RelativeLayout>

手動でSplashFragmentを追加する必要がどの手段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
            .add(R.id.viewpagerstart, new SplashFragment())
            .commit()
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=345402&siteId=1