Super.Oncreate(Savedinstancestate);

super.onCreate(savedInstanceState);

Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls.

Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity.

In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of the toString() method when extending java.lang.Object.

When we override a method, we have the option of completely replacing the method in our class, or of extending the existing parent class' method. By calling super.onCreate(savedInstanceState);, you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.

However, you must include this super call in your method, because if you don't then the onCreate() code in Activity is never run, and your app will run into all sorts of problem like having no Context assigned to the Activity (though you'll hit a SuperNotCalledException before you have a chance to figure out that you have no context).

In short, Android's own classes can be incredibly complex. The code in the framework classes handles stuff like UI drawing, house cleaning and maintaining the Activity and application lifecycles. super calls allow developers to run this complex code behind the scenes, while still providing a good level of abstraction for our own apps.

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.

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();
}
}
}

super.onCreate(savedInstanceState) error

You're trying to use a theme that it can't find. You can add it to your Android Manifest file with the following line.

android:theme="@style/Theme.AppCompat.Light"

super.onCreate(savedInstanceState) crashes in first run

This exception happens to me when I update the Android Studio from Canary 5 to canary 6

I return to Android Studio 2.3.2 and changed the class path back to Gradle 2.3.2 and the problem was solved.

Update: I tried with 3 of my applications and I get the same error, but solved it like described.

Crash on super.onCreate(savedInstanceState) after recreate activity

Seems to be an issue using the same id for the navigation graph and the destination.

https://issuetracker.google.com/issues/161825212

We had a very similar crash and solved it by using a unique id for the navigation graph.

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.



Related Topics



Leave a reply



Submit