Understanding how AndroidStudio generated code restores a PlaceholderFragment - android

I created a test application using AndroidStudio, selecting an activity with a fragment. What I do not understand is how the PlaceholderFragment is restored when savedInstanceState is not null, taking into account that setContentView is called after super.onCreate().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}

Hello the savedInstanceState is only not null when you rotated the device. When the activity recreate it should load from savedInstanceState. by the way the code you are talking about is part of android activity not android studio. Thank you.

Related

Impossible to restore fragment

I can't restore my fragment !
I'm saving like :
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
if(cameraFragment != null && cameraFragment.isAdded())
getSupportFragmentManager().putFragment(savedInstanceState, "cameraFrameLayout", cameraFragment);
super.onSaveInstanceState(savedInstanceState);
}
and restoring in onCreate(savedInstanceState) :
if(savedInstanceState == null)
cameraFragment = CameraFragment.newInstance();
else
cameraFragment = (CameraFragment) getSupportFragmentManager().findFragmentByTag("cameraFrameLayout");
When I change the device orientation I can successfully see that the onSaveInstanceState in called with my fragment but on the onCreate I have a null instance...
Txs for help !
You need to restore the saved fragment in onRestoreInstanceState
public void onRetoreInstanceState(Bundle inState){
cameraFragment = getFragmentManager().getFragment(inState,"cameraFrameLayout");
}
Also notice that getFragment is called to the FragmentManager instead if findFragmentByTag. Hope it helps!
Note: call super methods in onSaveInstanceState and onRestoreInstanceState overrides
In your fragment onCreate() method write setRetainInstance(true);
In the Activity that creates the fragment you should do something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
{
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, CameraFragment.newInstance())
.commit();
}
}
The android.R.id.content can be different in your case. This is where you want to load your fragment.

cant find code which changes Fragment

I'm working on my app since a few weeks. Now i pointed out the bug, that if i rotate my Device, it always replaces my Fragment to the "Home-Fragment".. and i dont know why. I Was searching it with "find in path"-function in Android Studio. I searched for keywords like:
-orientation
-setOrientation
-Landscape
-Portrait
But didnt find the code-snippet which changes my Fragment on rotation...
Any tipps? How can i find out where the "fragment replace" gets called?
the correct code to initialise the "home fragment" is like this:
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new HomeFragment())
.commit();
}
}
}
note that you only do the fragment transaction if savedInstanceState == null, if it's not null, the Android and Fragment frameworks will automatically recreate everything.
I'm pretty sure the reason that you're always replacing with the homeFragment is because you're not checking savedInstanceState == null
Add android:configChanges="orientation|screenSize" to the manifest in your activity. This will prevent your activity to recreate view after orientation changes.

Why is it necessary to check savedInstanceState inside of OnCreate? [duplicate]

This question already has answers here:
What's onCreate(Bundle savedInstanceState)
(5 answers)
Closed 7 years ago.
What is the purpose of the if block in the onCreate() method? Why is it necessary to check if savedInstanceState is null?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
When your activity is recreated, such as after a screen rotation or other configuration change, fragments are automatically reattached. By checking if savedInstanceState == null, you ensure that you are not re-adding a fragment that has already been added for you.
Well that's so you can remember where someone was when they last left your app. So for instance chrome remembers your last visited tabs.

findFragmentByID() returns null using container ID immediately after it is created

Note I've checked the very numerous "duplicates" of this question and none of them fit the bill, so please don't mark this as a duplicate.
I modified the default wizard-created app (in Android Studio) to try to find the placeholder fragment after it is created, like this (the only statement added is the Log line):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Log.d("", "Found fragment: " + getSupportFragmentManager().findFragmentById(R.id.container));
}
However the log just prints:
Found fragment: null
According to what I've read of the documentation and answers to similar questions, it should work. What's going on?
The fragment transaction has not yet been executed but just scheduled for later execution.
Wait for super.onStart() in the application lifecycle, of if you're impatient, call executePendingTransactions().

setOnClickListener generates Fragment exception

My App works great when I implement View.OnClickListener, ToolTipView.OnToolTipViewClickedListener, extending Activity context.
But, when I extend FragmentActivity instead of Activity as I mentioned, the app throws an exception.
Code is storagged onto
Supertooltips
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToolTipRelativeLayout toolTipRelativeLayout = (ToolTipRelativeLayout) findViewById(R.id.activity_main_tooltipRelativeLayout);
myToolTipView = toolTipRelativeLayout.showToolTipForView(
new ToolTip()
.withText("A beautiful View")
.withColor(Color.RED)
.withShadow(true)
.withAnimationType(ToolTip.ANIMATIONTYPE_FROMTOP),
findViewById(R.id.activity_main_redtv));
myToolTipView.setOnToolTipViewClickedListener(MainActivity.this);
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
[SOLVED]
I had override onCreateOptionsMenu and move all implementation there.
My problem was due to the repro deep in code

Categories

Resources