How to Call a Method in Another Activity from Activity

how to call a method(in activity(A)) from another activity(B) to work on that activity(A)?

You want to call a method which is in Activity A from Activity B. First of all, the interactivity communications are not advisable the way you have portrayed it.

But, there are ways you can achieve what you have asked.

  1. Start Activity For Result

If the Activity B is started from Activity A using StartActityForResult,

 //Activity A

val intent = Intent(this, ActivityB::class.java)
startActivityForResult(intent, 1001) //Any request code

You can get the result in onActivityResult callback on activity A.

override fun onActivityResult(requestCode: Int, resultCode: Int, data:Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1001 && resultCode == Activity.RESULT_OK)
callYourMethod()
}

  1. Pass a boolean in the intent. (This is kind of a hack)

When you click ok on the dialog in Activity B, call activity A by passing a boolean value in the intent.

 val intent = (this, ActivityA::class.java)
val bundle = Bundle()
bundle.putBoolean("KEY", true)
startActivity(intent, bundle)

And in Activity A's onCreate method, get the bundle from intent, and if it is true, then call the method.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val isTrue = intent.getBooleanExtra("KEY", false) //false is default value
if(isTrue)
callYourMethod()
}

There's also another way to communicate between classes like from adapter to Activity, fragment to activity etcetera using Interface. But, I hope the above-mentioned steps will help you for now.

Calling function from another activity

Add the static keyword to your shared methods

public static void createTable()

Then use:

Data.createTable();

somewhere in another Class / Fragment / Activity.

I do so and it works.

Well, my "Data" is a normal Class, not an Activity, but I also have an Activity with shared methods in the very same way.

This is my dbTableCreate method:

// Create the database table if it doesn't exist
protected final static void dbTableCreate(final Context ctx)
{
SQLiteDatabase db = null;
try
{
db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
db.execSQL
(
"CREATE TABLE IF NOT EXISTS " + DB_TABLE +
" (date DATETIME PRIMARY KEY NOT NULL DEFAULT (CURRENT_DATE), " +
"score INTEGER DEFAULT (null));"
);
}
catch (final SQLiteException se)
{
System.out.println
(
ctx.getClass().getSimpleName() + "\n" +
"Could not create the database"
);
se.printStackTrace();
}
finally
{
db.close();
}
}

And I call it like:

Context ctx = getApplicationContext();
CLS_DB.dbTableCreate(ctx);

The main difference between my class and yours is that mine is declared as

public final class CLS_DB
{
/* ----------------------------- Constants ------------------------------ */

private final static String DB_NAME = "pat.db";
private final static String DB_TABLE = "tests";

/* ------------------------------ Methods ------------------------------- */

And not as an Activity.

I'm not initializing the Class, and it has no constructor.

It's just a bin of shared (and not, for doing operations not seen anywhere else in my code) methods.

How to call a non-static method from one Activity to another activity

class A extends Activity{
static A instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}

public static A getInstance() {
return instance;
}
....
public void clear(){}

}

and in class B:

class B extends Activity {
public void clearData(){
A a = A.getInstance();
a.clear();
}
}

How to call a method of another activity in Kotlin?

for a quick solution, You can declare a static variables for data that You received in onNotificationReceived of ControlActivity

To crate a static variables, You will define those variables inside companion object

And you can get that data via class name like ControlActivity.foo inside your OnlineActivity (where foo is the static variable inside ControlActivity)

Call Activity METHOD from another Activity

To all experts that thought this question was irrational, thank you very much.

As for my noobies, if you would like to use the same buttons on all activities without copy and paste of same code, I found this solution to work:

public class MainActivity extends BottomButtons {
....
bottomButtons();

Your OTHER activity (and all other activities) should extend BottomButtons then you just call the bottomButtons method in your onCreate of those activities and then you will have access to the buttons including their onClick events.



Related Topics



Leave a reply



Submit