Anup Rojekar
Anup Rojekar

Reputation: 1103

Creating of multiple instance of the same Activity?

I am creating a chat application & for that purpose i have used TabHost.

In that first tab contains List of Buddies, and as soon as user clicks on any of the buddy

from buddy it should create another tab for that buddy in order to chat.

I am completed up to this but my problem is I am using a single Activity to perform Chat

but It always shows the same activity for each buddy.

Any Help will be highly appreciated. Here is my code,

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {

    RosterEntry entry = List.get(position);

    String userName = entry.getName();

    Intent intent = new Intent().setClass(RosterScreen.this,
            com.spotonsoft.chatspot.ui.ChatScreen.class);

    TabSpec tabSpec = Home.tabHost.newTabSpec("chat")
            .setIndicator(userName).setContent(intent);

    Home.tabHost.addTab(tabSpec);

}

Best Regards,

~Anup

Upvotes: 0

Views: 515

Answers (2)

Selvin
Selvin

Reputation: 6797

in onCreate of ChatScreen you only setup basic stuff like getting View and store it in private fields

onResume you "recreate" ChatScreen with buddy-specific data ... how to do this (pls, read comments in code)?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;

public class ChatScreen extends Activity {
    TextView textview = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        textview = new TextView(this);
        setContentView(textview);
    }

    @Override
    public void onResume(){
        super.onResume();
        Intent intent = getIntent();
        if(intent!=null){
                    //we read buddy-specific data here
            textview.setText(intent.getStringExtra("chatwith"));
                    //we only setting textview with user name
                    //in real app you should store conversation somewere (fx in db)
                    //and load it here
        }   
    }
}

and your code

Intent intent = new Intent().setClass(RosterScreen.this, com.spotonsoft.chatspot.ui.ChatScreen.class);
// you shoud add this line and provide some information fx useName or userID to ChatScreen Activity
intent.putExtra("chatwith", userName);
TabSpec tabSpec = Home.tabHost.newTabSpec("chat").setIndicator(userName).setContent(intent);
Home.tabHost.addTab(tabSpec);

Upvotes: 1

THelper
THelper

Reputation: 15619

You can add data to your intent before starting it, for example

intent.putExtra("user", userName);

In the onCreate of your activity you can read this data and use it to setup your activity.

Also, make sure that you have set the proper launchmode for your activity.

Upvotes: 1

Related Questions