Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How can I pass activities to a function in another class?

Posted on November 11, 2022 By

Solution 1 :

To answer the titled question, I think you’d have to pass an Intent to the method, which in your case doesn’t make a whole lot of sense, e.g.,

Intent intent = new Intent(getApplicationContext(), Home.class);
shopMng.someMethod(intent);

To start the activity, you still need some context, so this should be disregarded as the solution, but could answer the titled question for others.

The standard way is to pass context or the activity itself, so to get your program working correctly, try this instead:

public class ShopManager {

    public void toast(Context context) {
        Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();
    }

}

or respectively:

public class ShopManager {

    public void toast(Home home) {
        Toast.makeText(home, "Test", Toast.LENGTH_SHORT).show();
    }

}

That’s it! Your Home activity is already calling your toast method in your ShopManager class correctly. Good luck!

Solution 2 :

make a static method in your ShopManager class that get context and shows toast like this:

public class ShopManager {
    static void toastMessage(Context context){
        Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();
    }
}

for calling it from your activity do this:

ShopManager.toastMessage(YourActivity.this);

Problem :

I am building a shop project for school, and as I decided that instead copying and pasting a function which toasts that there’s no internet connection in each activity, I should instead add it to my project manager class, which is not an activity, ShopManager. The problem is, in order to toast, one of the parameters is the activity in which the toast should occur and I have not found a way to do it. I decided for now I should just try to toast before adding any internet checking, and this is what I created thus far:

This is in my Home Page activity:

public class Home extends AppCompatActivity implements View.OnClickListener {

    ShopManager shopMng;
...
...
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        shopMng = new ShopManager();

        shopMng.toast(Home.this);
   }
}

In my ShopManager class:

public class ShopManager {

    public void toast(Class cls) {
        Toast.makeText(cls.this, "Test", Toast.LENGTH_SHORT).show();
    }

}

The error message I am getting is ” ‘toast(java.lang.Class)’ in ‘com.example.birthdayshop.ShopManager’ cannot be applied to ‘(com.example.birthdayshop.Shop)’ “, which I deducted basically means that the type of the cls parameter is not correct. What type of variable should it be in order to work in ALL activities? Thank you so much in advance.

READ  [FIXED] gradle - How to solve could not find method compile() for arguments com.android.support:recyclerview-v7:25.3.1?
Powered by Inline Related Posts

Comments

Comment posted by stackoverflow.com/questions/36826004/…

stackoverflow.com/questions/36826004/…

Android Tags:android, android-studio, java, xml

Post navigation

Previous Post: [FIXED] Unable to connect Firebase in Android Emulator for Flutter Project
Next Post: [FIXED] android – Highlight only one Card in Compose through selection

Related Posts

[FIXED] android – Is onResume of activity guaranteed to be called? Android
[FIXED] android – Refresh AppWidget with network request using RemoteView button Android
[FIXED] android – How to add http header to requests made by OSMDroid? Android
[FIXED] c# – Xamarin Forms Get Object element on image click Android
[FIXED] android – Hardcoded string “blablabla”, should use @string resource Android
[FIXED] android – Flutter: TCP socket connection fails on real device Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme