Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call An Activity class from a noraml java class in Android [duplicate]

I have one noraml java class say ReceivedChat.java in the constructor of this class i want to call an Activity of Android.

public class ReceivedChat {
    String message;
    String from;
    Context context;

    ReceivedChat(String message, String from) {
        this.message = message;
        this.from = from;

        Bundle b = new Bundle();
        b.putString("message", this.message);
        b.putString("from", this.from);
        b.putString("fromChat", "true");

        Intent i = new Intent(context.getApplicationContext(), XmppChatActivity.class);
        i.putExtras(b);
        context.getApplicationContext().startActivity(i);
    }
}

My Activity class is XmppChatActivity.

This program is not working. it is not calling the onCreate of my XmppChatActivity class Any help will be thankfull to me.

like image 765
user2139898 Avatar asked Dec 08 '22 17:12

user2139898


1 Answers

how to call An Activity class from a normal java class

you will need to pass Current Activity Context to ReceivedChat at the time of Object Creation from an Activity or any other Application Components as :

ReceivedChat(String message, String from,Context context)
{
this.message = message;
this.from = from;
this.context=context;  //<< initialize Context here 
Intent i = new Intent(context,XmppChatActivity.class);
 //....your code here
context.startActivity(i);

}

and instead of starting another Activity from class Constructor create an method in ReceivedChat and call it after object creation

like image 85
ρяσѕρєя K Avatar answered Dec 11 '22 11:12

ρяσѕρєя K