0

Hi I am kind of new to android, still learning. And my problem is that, for example I have a method which was created in the MainActivity and I need to call it from another class.

Is it a good practice to get the instance of the MainActivity so that I may be able to call the method in the MainActivity from another class?

This is an example:

public class MainActivity extends AppCompatActivity {

   private static MainActivity inst;

public static MainActivity instances()
{
    return inst;
}

@Override
public void onStart() {
    super.onStart();
    inst = this;
}

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void showToast (String text){
    Toast.makeText(inst, text, Toast.LENGTH_SHORT).show();
}

Then this is the other class:

public class broadcastReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

  MainActivity instance = new MainActivity();

 instance.showToast(AnyText);
    }
}

I saw this type of coding while looking at tutorials and wondered if it's a good practice or maybe there might be a better way? Since I get the warning of Do not place Android Context Classes in static classes

Thanks in advance for any insight or help! :D

  • I think you can directly create instance wherever you want using `MainActivity instance = new MainActivity();` Don't need to return instance of any class using any method. – Jaydip Kalkani Mar 11 '18 at 07:54
  • 1
    Possible duplicate of [Get activity instance](https://stackoverflow.com/questions/9723106/get-activity-instance) – Ali Mar 11 '18 at 08:05

4 Answers4

0

I guess You want to make A singleton of Activity Class

but as Mention in All Pattern Design

using Singleton

If and Only If its only way to Make A Global Variable

Singleton is based on Lazing Initialing and Load On Memory

so I guess If you cant to Interact With Activiy You can Use

BroadCast Or Intents

Community
  • 1
  • 1
Elsunhoty
  • 1,609
  • 14
  • 27
0

Is it a good practice to get the instance of the MainActivity so that I may be able to call the method in the MainActivity from another class?

You totally can do this but you don't need to make it static and use a constructor. Just create a new instance like follows and you'll access the public methods

MainActivity mainActivity = new MainActivity();
mainActivity.showToast(text);

About the warning

It suggests avoiding having context fields defined as static. The warning itself explains why: It's a memory leak. If you make it static it will be accessible anywhere in your app and some methods can hold the reference to this context for a really long time and it won't be garbage collected. It will lead to a outofmemory exception and the app could crash. But here you're trying to invoke showToast() from broadcastreceiver so you can just get rid of static references. And it you need them in the future you safe ways to inject context

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
0

You can call method from another class like this:

MainActivity instance = new MainActivity();
String data = instance.data();

and create data method in that class:

public String data() {
    return mangaId;
}
chunkydonuts21
  • 79
  • 1
  • 10
0

You cannot create instances of an Activity using the new operator.

You have to use an Intent to let an Activity to be created.

So you cannot get a reference to an instance of your activity.

The only methods you can use of your activity class are static ones.

greenapps
  • 11,154
  • 2
  • 16
  • 19