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" />