2

I have an activity which should display Fragment only in portrait mode. So, my portrait layout have container-view and my landscape - don't. However, after changing orientation portrait->landscape Fragment is recreated even tho it have no container to be displayed in.
Furthmore, this re-created Fragment's is truly alive and responds to all lifecycle events s.e onPause, onResume, etc.

Log:

A{6bee548 #2 id=0x7f07002c tag} - ON_CREATE
A{6bee548 #2 id=0x7f07002c tag} - ON_START
A{6bee548 #2 id=0x7f07002c tag} - ON_RESUME
*** orientation change ***
A{6bee548 #2 id=0x7f07002c tag} - ON_PAUSE
A{6bee548 #2 id=0x7f07002c tag} - ON_STOP
A{6bee548 #2 id=0x7f07002c tag} - ON_DESTROY
Activity: Nowhere to attach!
A{bf65baf #2 id=0x7f07002c tag} - ON_CREATE
A{bf65baf #2 id=0x7f07002c tag} - ON_START
A{bf65baf #2 id=0x7f07002c tag} - ON_RESUME

How can I avoid creation of Fragment after orientation change?

Here's all I have:

public class MainActivity extends AppCompatActivity {

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

        View container = findViewById(R.id.container);
        if (container != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(container.getId(), new A(), "tag")
                    .commit();
        } else {
            Log.d(MainActivity.class.getSimpleName(), "Nowhere to attach!");
        }
    }

    public static class A extends Fragment {

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleEventObserver() {
                @Override
                public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                    Log.d(A.class.getSimpleName(), String.format("%s - %s", A.this.toString(), event.name()));
                }
            });
        }
    }
}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">


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

</androidx.constraintlayout.widget.ConstraintLayout>

layout-land.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Nothing here"/>

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:text="Fragment" />
Paul Freez
  • 1,059
  • 15
  • 27
  • You mean ,you want to prevent recreation of fragment when orietantion change occres? – Kabir Aug 15 '19 at 12:16
  • @Kabir , yes, exactly. But adding ```setRetainInstance(true)``` won't help and would only cause IllegalStateException because it'd try to attach fragment to unexisting view. – Paul Freez Aug 15 '19 at 12:18
  • Ok,but you can also use onSaveInstanceState to save fragment state. – Kabir Aug 15 '19 at 12:20
  • I don't need to save it - I need to ignore it, so it won't be created again. – Paul Freez Aug 15 '19 at 12:41
  • Then i have only two way setRetainInstance(true) inside onCreate() of fragment.And you can save state of fragment,and get back that fragment when orientation occurs. – Kabir Aug 15 '19 at 12:47
  • @Kabir , as I wrote before: adding ```setRetainInstance(true)``` in Fragment's ```onCreate(...)``` would cause exception in landscape orientation - because I don't have (and need) view container to put fragment into. And I do not need to save state of fragment - I need it to be destroyed without any state saving or further re-creation. – Paul Freez Aug 15 '19 at 12:55

1 Answers1

0

Current solution I've got is to pass null in Activity's onCreate(...) method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

But this looks risky because I may be not the only who uses saved instance state to actually store states.

Paul Freez
  • 1,059
  • 15
  • 27