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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With