I have 3 classes with the same function. The class H is a controller class, so i have made a setactivity class to get the instance of the activity that called it. Now i have to call the function userleft() of the currently initialized class from the controller class H which is a non Activity.
The error i am getting is "The method userleft() is undefined for the type Activity" and i know that the exception would come, since the class Activity does not contain a method called userLeft(). But what is the best way to make this work?
See the structure of the app below.
public class A extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class B extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class C extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
and i have another non activity class
public class H {
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(Activity activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Thanks!
You need to redesign your classes like below
Declaire a base activity class
public abstract class BaseActivity extends Activity {
public abstract void userleft();
}
And declare your classes like
public class A extends BaseActivity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
#Override
public void userleft() {
}
}
And now modify your Non-activity class like
public class H {
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(BaseActivity activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Now you will be able to call your method for any subclass activity.
In your randomCallBack() method you have an Activity object, the class Activity does not contain a method called userLeft(), so it is normal that you get such an exception. So, you have two ways of solving this issue:
Either, you cast the activityGame to its corresponding class as:
public void randomCallBack(){
if (activityGame instanceof A) {
((A)activityGame).userleft();
}
if (activityGame instanceof B) {
((B)activityGame).userleft();
}
if (activityGame instanceof C) {
((C)activityGame).userleft();
}
}
Or, in a better way, you create an Interface, which is implemented by your clasess A, B, and C, and used by H class. Like this:
public interface UserLeft {
public void userleft();
}
public class A extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class B extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class C extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class H {
UserLeft activity;
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(UserLeft activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Activity class does not have the method of userleft , so that you can't call that method by the reference activity . You can define one BaseActivity with the method userleft as the parent class of these classes.
You forgott your instance variable declaration :) private Activity activityGame = null;
Of course class Activity has no method called userleft().
You need to cast your variable activityGame to your specific Class like that:
public class H {
private Activity activityGame = null;
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(Activity activity) {
this.activityGame = (<CAST TO YOUR CLASS>) activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
EDIT:
Please try this:
public void setActivity(Activity activity) {
this.activityGame = (activity.getClass()) activity;
}
Related
I have a video player app where I need to access the lifecycle of an abstract activity from another class in Android. In my abstract activity, I've tried using LifecycleRegistry, but this is getting me the lifecycle owner not the actually lifecycle of the abstract class. How can I access the lifecycle of an abstract activity from another class?
Here is my abstract activity:
abstract public class MainActivity extends AppCompatActivity {
private LifecycleRegistry lifecycleRegistry;
VideoPlayer videoPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lifecycleRegistry = new LifecycleRegistry(this);
lifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
setContentView(R.layout.activity_main);
videoPlayer = new VideoPlayer();
playVideo();
}
public void playVideo(){
videoPlayer.init();
//calls function in VideoPlayer class
}
#Override
protected void onResume() {
super.onResume();
lifecycleRegistry.setCurrentState(Lifecycle.State.RESUMED);
}
#Override
protected void onDestroy() {
super.onDestroy();
lifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
}
}
Here is the class where I need to get the lifecycle of my abstract MainActivity:
public class VideoPlayer {
public void init() {
playVideo();
}
public void playVideo() {
//async call happens here, I need getLifeCycle() from MainActivity
}
}
Don't know a know about the context of you feature, but you You can do smth
public class VideoPlayer {
private Lifecycle mLifecycle;
public VideoPlayer(Lifecycle lifecycle) {
mLifecycle = lifecycle;
}
public void init() {
playVideo();
}
public void playVideo() {
//you have mLifecycle now
}
}
In Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
videoPlayer = new VideoPlayer(getLifecycle());
}
I have an Activity which creates a class that does some work. What is the typical Android method of having this class report back to the Activity in order to update the UI?
My activity, which creates the class:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyClass obj = new MyClass(this);
obj.DoWork();
}
}
The class that does the work, and wants to report back some
public class MyClass(Context context) {
private Context context;
public void DoWork() {
//Do some work with a countdown timer
//Report back some values
}
}
You can create your own interface like this:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyClass obj = new MyClass(this);
obj.setOnWorkDoneListener(new MyClass.OnWorkDoneListener(){
#Override
public void onDone(Values values) {
//Work done, use values
updateUI(values);
}
});
obj.DoWork();
}
}
public class MyClass(Context context) {
private Context context;
public interface OnWorkDoneListener{
void onDone(Values values);
}
private OnWorkDoneListener listener;
public void setOnWorkDoneListener(OnWorkDoneListener listener){
this.listener = listener;
}
public void DoWork() {
//Do some work with a countdown timer
when(workEnded) listener.onDone(backValues);
}
}
I want to call method of Other activity. I searched alot and tried by myself but failed. It work when i call from class to class like classA m=new classA();
m.function();
But i want to call from another activity method. Please help
public void onNotificationPosted(StatusBarNotification sbn) {
ChatActivity ch=new ChatActivity();
ch.location();
}
public class ApplicationContext extends Application {
public ActivityMain activityMain;
public void setActivityMain(ActivityMain activityMain) {
this.activityMain = activityMain;
}
public ActivityMain getActivityMain() {
return activityMain;
}
In ActivityMain.class
public class ActivityMain extends BaseProject implements MainView {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApplicationContext.getInstance().setActivityMain(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
ApplicationContext.getInstance().setActivityMain(null);
}
Use it anywhere like Activity, Fragments, Broadcast Receiver or Service like
if (ApplicationContext.getInstance().getActivityMain()!=null){
ApplicationContext.getInstance().getActivityMain().callAnyMethod();
}
Enjoy :)
How can I start getdata() method to another class. I want to show the output to a `ListView which is in another Java class.
tab1Background.java
public class tab1Background extends Activity{
ListView lv;
lv = (ListView) findViewById(R.id.lv);
//some code here
public void getdata() {
//some code here
}
}
tab1report.java i want to start the getdata() activity to show data in listview
public class tab1report extends Fragment {
//some code here
}
public class tab1report extends Fragment {
public void startSomething()
{
((tab1Background) getActivity()).getdata();
}
}
You could use callback method.
public class tab1report extends Fragment {
Callback callback;
public interface Callback {
void call();
}
onAttach(Context context) {
callback = (Callback) context;
}
onStart() {
// Some code here
callback.call();
}
}
public class tab1Background extends Activity implements tab1report.Callback {
ListView lv;
lv = (ListView) findViewById(R.id.lv);
//some code here
public void getdata() {
//some code here
}
public void call() {
getdate();
}
}
This will ensure that the functionality is passed back to the activity.
Why can't I access remTime object? I get runtime exception while I can access all other Activity members declared the same way.
public class MainActivity extends Activity {
ListView lstDayInterval, lstWeekDay, lstMonthDay, lstMonth;
...
RemCalendar remTime;
#Override
public void onCreate(Bundle savedInstanceState) {
...
remTime.setToNow();
}
public class RemCalendar {
private Calendar C;
public RemCalendar() {
C = Calendar.getInstance();
}
public void setToNow() {
C = Calendar.getInstance();
}
}
Thanks
you forget to initialize remTime instance before calling method from RemCalendar class. initialize it inside onCreate :
#Override
public void onCreate(Bundle savedInstanceState) {
...
remTime=new RemCalendar();
remTime.setToNow();
}