What's Oncreate(Bundle Savedinstancestate)

What's onCreate(Bundle savedInstanceState)

If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState), it can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

... you
should use the onPause() method to write any persistent data (such as
user edits) to storage. In addition, the method
onSaveInstanceState(Bundle) is called before placing the activity in
such a background state, allowing you to save away any dynamic
instance state in your activity into the given Bundle, to be later
received in onCreate(Bundle) if the activity needs to be re-created.
See the Process Lifecycle section for more information on how the
lifecycle of a process is tied to the activities it is hosting. Note
that it is important to save persistent data in onPause() instead of
onSaveInstanceState(Bundle) because the latter is not part of the
lifecycle callbacks, so will not be called in every situation as
described in its documentation.

source

What usage does savedInstanceState have in super.onCreate(savedInstanceState)?

The linked question/answer in @sasikumar's comment is good, and answers why you must call super.onCreate(), but it does not answer why you must pass the Bundle savedInstanceState parameter to the super call (as opposed to a new Bundle() or null).

The simple answer is that the savedInstanceState bundle contains information recorded by the Android framework that the super method will then use to restore state. For instance, any EditText with an android:id attribute will automatically save whatever the user has typed into it, and that information will be inside the savedInstanceState bundle. If you pass null, this automatic restoration is impossible.

Note that the savedInstanceState parameter will actually be null the very first time your activity is created. It will only be non-null when your activity has been destroyed and re-created (usually in response to a configuration change like the rotation of your phone, but there are many other scenarios as well).

Update

My example of automatic state restoration was incorrect. The EditText automatic restoration happens in the onRestoreInstanceState(Bundle savedInstanceState) callback, not in onCreate. Unfortunately, you can't simply pass null to that super method to see what happens (your app will crash).

However, this example will show what I meant. Assume your activity_main has some FrameLayout with an id of @+id/content, and assume you have some Fragment subclass that displays anything visible to the user. Try this:

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

if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content, new MyFragment(), "MyFragment")
.commit();
}
}

As is, this will display your fragment on first launch, and will continue to display the fragment after you rotate your device back and forth. This is because the FragmentManager uses the savedInstanceState bundle to save/restore fragments, and that is done in super.onCreate.

If you change the super call to super.onCreate(null), you'll still see the fragment the first time your activity starts up (since we're manually adding it at that point). But if you rotate the device, the fragment will disappear.

onCreate(Bundle savedInstanceState) is already defined

You have a duplicate declaration of onCreate(). You need to combine them both to make one something like:

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

WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://usreport.net/");
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.toString());
return true;
}
});

button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {

Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
}

You need to learn to read the error and evaluate the actual issue. Things may appear complicated while you are getting started with coding, but I suggest you to go through the basics of Java before getting into Android development. Trust me it will eventually save you a lot of time.

Two oncreate(bundle savedinstancestate) in the same activity?

Remove one OnCreate(....)

Try Below Code:

public class Dash extends AppCompatActivity {
private DrawerLayout drawer;

Button signout;
ChipNavigationBar chipNavigationBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
signout=findViewById(R.id.logout);
drawer = findViewById(R.id.drawer_layout);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),Existing.class));
finish();
}
});

chipNavigationBar = findViewById(R.id.bottom_nav_menu);
chipNavigationBar.setItemSelected(R.id.bottom_nav_menu, true);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new NewsFragment()).commit();
bottomMenu();
}

private void bottomMenu() {

chipNavigationBar.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {
@Override
public void onItemSelected(int i) {
Fragment fragment = null;
switch (i) {
case R.id.bottom_nav_menu:
fragment = new HomeFragment();
break;

case R.id.bottom_nav_news:
fragment = new NewsFragment();
break;

case R.id.bottom_nav_profile:
fragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
}
});

}

@Override public void onBackPressed(){
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}

Why is it necessary to check savedInstanceState inside of OnCreate?

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.

Where is Bundle object created in onCreate(Bundle savedInstanceState)

If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState), it can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
}

The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will get passed in to onCreate() and also onRestoreInstanceState() where you'd extract the values like this:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}


Related Topics



Leave a reply



Submit