Why do OnCreate should be called only once on the start of Activity?
Asked Answered
M

7

7

I would like to know, why OnCreate() is called only once at the start of an activity?

Can we call OnCreate() more than once in the same activity?

If yes, than how can we call it? can anyone give an example?

Thanks a lot!!!

Markel answered 20/12, 2012 at 3:40 Comment(6)
what about using fragment.. "developer.android.com/reference/android/app/Fragment.html"Avirulent
I think you can call onCreate manuallyUnstick
@nick, how can we calll.. can you explain me briefly??Markel
I just tried. Ill post the sample code. But please tell us why you will want to do this.Wivinah
for THread purpose i just wanted to call that method once more soMarkel
For same reason you don't call public static void main(String...args) again and again. It's needed only to begin.Patiencepatient
W
6

Why would you want to called it again? unless the activity is reconstructed, which is called by system. You cannot call OnCreate manually , it is the same reason why you won't call setContentView() twice. as docs:

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically. Once you finish init your widgets Why would you?

UPDATE I take some words back, you CAN do this manually but I still don't understand why would this be called. Have you tried Fragments ?
Samplecode:

public class MainActivity extends Activity implements OnClickListener {
        private Button btPost;
        private Bundle state;
        private int counter = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            state = savedInstanceState;
            btPost = (Button) findViewById(R.id.btPost);
            btPost.setOnClickListener(this);
            Toast.makeText(getBaseContext(), " " + counter, Toast.LENGTH_LONG)
                    .show();
        }

        @Override
        public void onClick(View v) {
            counter++;
            this.onCreate(state);
        }
    }
Wivinah answered 20/12, 2012 at 3:44 Comment(0)
T
1

onCreate() method performs basic application startup logic that should happen only once for the entire life of the activity . Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession.

The initialization process consumes lot of resources and to avoid this the activity once created is never completely destroyed but remains non visible to user in background so that once it is bring back to front , reinitialization doesn't happen .

Toland answered 20/12, 2012 at 3:49 Comment(0)
U
1

Where you want to call onCreate manually.

Then just do this.

    finish();
    Intent intent = new Intent(Main.this, Main.class);
    startActivity(intent);

finish() calls the current stuff.

And if you are doing somethong getExtra in this activity then do this,

@Override
    protected void onSaveInstanceState(Bundle outState) {

            outState.putString("key",your_variable);
        super.onSaveInstanceState(outState);

    }

And add this to your onCreate()

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
if(savedInstanceState != null)
        {
            your_variable= savedInstanceState.getString("key"); 
        }
}
Unstick answered 20/12, 2012 at 4:1 Comment(0)
O
0

Why would you want to call onCreate more than once? You will be re-creating the activity. If this is what you need for whatever reason then finish the activity and use an intent to create a new instance of that activity. Otherwise, you have two instances of the activity at the same time. Hope that helps but if that doesn't make sense then add more information as to what you want so we have context

Optics answered 20/12, 2012 at 3:44 Comment(0)
K
0

OnCreate is basically use to create your activity (UI). If you have already created your activity then you need not create it again as you have already created.

It is basically used to initialize your activity and to create user interface of your activity. Activity is a visual part which you can use again and again so.. I think your problem is not to recreate activity but to reinitialize all components of your activity. For that purpose you can create a method initialize_act() and call it from anywhere...

Kristalkristan answered 20/12, 2012 at 3:51 Comment(0)
G
0

@OnCreate is only for initial creation, and thus should only be called once.

If you have any processing you wish to complete multiple times you should put it elsewhere, perhaps in the @OnResume method.

Giuliana answered 20/12, 2012 at 4:1 Comment(0)
P
0

Recently i realized that onCreate is called on every screen orientation change (landscape/portrait). You should be aware of this while planning your initialization process. Recreation can be suppressed in AndroidManifest.xml:

<activity
            android:configChanges="keyboardHidden|orientation"
            android:name=".testActivity"
            android:label="@string/app_name"></activity>
Pugilism answered 26/3, 2016 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.