Skip to content Skip to sidebar Skip to footer

How To Call Method In Another Activity

I should call a method from a activity to another activity. my firstclass is: public class firstclass extends Activity { public String Kind(){ SharedPreferences preferences = P

Solution 1:

If you have code that needs to be shared between activities, you should export it to a Helper class.

Example:

publicclassKindUtils {
    publicstatic String Kind(Context context){
SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(context);
    booleanKey= preferences.getBoolean("Key", true);
    if(Key){
        name="you";
    }
    else{
        name="me";
    }
    return name;
    }
}

Now you can call KindUtils.Kind(this)in both activites.

Solution 2:

In short: you shouldn't. Make a separate (singleton) class which handles the kind() logic which both activities can access.

Solution 3:

That's not how you do it. Let me correct you code as per my knowledge.

  1. Always make sure that class name should start with Capital letter its java class naming convention
  2. Always use camel casing for methods name, starts with small caps.
  3. You can't use keyword new for class that extend Activity.

Now how can you access the method of other activity from some activity

((Firstclass)getActivity()).kind;

That's how you access the method from other class in android.

publicclassFirstclassextendsActivity {
    public String kind(){
SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(this);
    booleanKey= preferences.getBoolean("Key", true);
    if(Key){
        name="you";
    }
    else{
        name="me";
    }
    return name;
    }
}

in Second Class you'll do

publicclassSecondclassextendsActivity{
    publicvoid take(String token, int transactionId) {
        String str= ((Firstclass)getActivity()).kind;
   }
}

Hope it will help

Solution 4:

First of all the looper error happens when you call a UI (user interface) method from a non UI thread. In this case because the second activity was not started as an intent, you started it as new firstClass() bad things will happen.Activitys have lifecycle callbacks. onCreate to pass refrences to what items should be drawn usually at least a refrence to a layout setContentView(R.layout.activity_main);, onStart is to start drawing. Keep in mind Android supports many screen sizes and must ask what you want put into the activity, how many, android measures and figures how to display then calls onstart to display it and so on. The main thing is start Activitys as intents and read the link. In the manifest Activity block you have Launcher which means put an icon on the display to start an Activity when the icon is clicked. A special way of starting an activity as an intent.

Solution 5:

You simply don't instantiate instances of Activities in Android like you would do in Java. You should create Intents and call startActivity(). To make your logic work, I suggest that you use BroadcastReceivers.

Post a Comment for "How To Call Method In Another Activity"