Strife
Strife

Reputation: 69

Working between Activities in android

Okay I am really new to android development and looked around for a clear explanation but couldn't find much. I'll try to be as clear as possible in my question.

Basically, lets say I have 2 Activities, a Create Activity which contains a form with text boxes, etc. (information the user fills out), and a create button on the bottom. My second activity called Creations is basically empty visually until a user creates something with the form in the Create Activity.

So I have a method for when the create button is clicked in the Create Activity

 public void create(View view){

    Creations.make(info1, blah1, blah2, etc);

}

Now this make() method is in the Creations Activity, it draws a custom view on that page with the info submitted and I want it to be called every time the user clicks the create button in the Create Activity. I know I cannot do this unless make() is a static method, but then how else would I implement this? I know I would have to make an object of my Creations Activity but then wont I have to make multiple objects of the same activity for every new item I want to add?

Upvotes: 0

Views: 120

Answers (1)

Rupesh
Rupesh

Reputation: 3505

basically you do not need to create an explicit object of an activity, you just need to start an activity with the api startActivity();

now, in your case,

there will be a method onCreatePressed() in CreateActivity as shown below,

public void onCreatePressed(View v) {
    Intent intent = new Intent(CreateActivity.this, CreationActivity.class);
    intent.putExtra(KEY_INFO, info);
    intent.putExtra(KEY_BLAH1, blah1);
    .
    .

    CreateActivity.this.startActivity(intent);
}

and in CreationActivity you will have to override onCreate() method, which would look like

public void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();

    /* if info type is of int there is a method getIntExtra and so on, 
     * if it is a custom class then it must implement Serializable interface 
     * and there is method getSerializableExtra for this.
     */ 
    InfoType info = intent.get<InfoType>Extra(KEY_INFO);
    .
    .
    .

    // setContentView(some_resource_id);

    // inflate it with the data. 
}

Upvotes: 1

Related Questions