1. Download our Official Android App: Forums for Android!

Apps Oncreate function

Discussion in 'Android Development' started by cinojose, Jul 15, 2011.

  1. cinojose

    cinojose Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jul 15, 2011

    Jul 15, 2011
    1
    0
    5
    Guys, i got two activities in my app. in one they are downloading content from a web resource, the other activity is just the details about an item of activity 1 , so once i go to my activity 1 from my activity 2 , the oncrete method is still invoking.?how to make the oncreate to be just invoked only once i created the activity?
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    Welcome to AF!

    If you are switching between activities, you cant really controll when the API will call onCreate()

    You will just have to plan around it. You might want to read about activity lifecycles here:

    Activities | Android Developers
     
  3. parada

    parada Lurker
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Nov 23, 2011

    Nov 23, 2011
    2
    0
    5
    Hello, Everybody.
     
  4. parada

    parada Lurker
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Nov 23, 2011

    Nov 23, 2011
    2
    0
    5
    Dear Sirs and Madams,

    I've had same problem (onCreate is called more than once), but in just one actvity. It occurs when phone is rotated. Is it normal? Can onCreate be called more than once in same activity?

    Bellow, a simple test I've made to confirm it. There is just a button with id=button1 created via eclipse IDE. Every time I rotate phone, text of button returns to "123".

    Source code:

    package toc.p;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class ToncreatActivity extends Activity {

    Button b1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button b1 = (Button) findViewById( R.id.button1 );

    b1.setText( "123" );

    b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    b1.setText( "abc" );
    }
    });
    }
    }


    The layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:eek:rientation="vertical" >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

    </LinearLayout>
     

Share This Page

Loading...