logo
down
shadow

Are there any cons of creating Singleton class with static field to communicate between activities in Android?


Are there any cons of creating Singleton class with static field to communicate between activities in Android?

Content Index :

Are there any cons of creating Singleton class with static field to communicate between activities in Android?
Tag : android , By : Brandon
Date : November 27 2020, 11:01 PM

hop of those help? IMHO, there are always cons in using Singleton design pattern in your applications. Some of them are (from the top of my head):
Coupling between otherwise unrelated objects and flows through Singleton's instance Emergence of a "global state", which makes debug a lot harder Inability to mock static fields and methods through "conventional" mocking The fact that a reference to Singleton can be easily obtained in any part of the application leads to a total mess (people stop thinking about dependency graph) Singletons tend to breed: you introduce one, then another one, then you find yourself with 10 singletons which hold app's state in a "global cloud state"

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Cannot make a static reference to the non-static field when creating a singleton Manager


Tag : java , By : christiandsg
Date : March 29 2020, 07:55 AM
help you fix your problem When you prefix a variable with a class name you are telling Java the variable is static. Since the variable is not static it is giving you an error. The code should read:
public void resetGame(){
    mCurrentScore = INITIAL_SCORE;
}

Reflection get field of singleton static nested class


Tag : java , By : DarrenBeck
Date : March 29 2020, 07:55 AM
wish of those help I've got the following class: , After
            Field f = c.getDeclaredField("instance");
            f.setAccessible(true);
            Object instance = f.get(null); // null as static, no 'this'.
                map = (ArrayMap<String, Integer>) f1.get(instance);

singleton or class with static field


Tag : java , By : Pieter Taelman
Date : March 29 2020, 07:55 AM
it fixes the issue You could use a enum with a single enum value as each enum value is out of the box a singleton.
The enum could implement an interface to be testable and in order to be able to switch to another implementation if required :
public enum FileBufferedProcessorService implements FileProcessorService {

    SINGLETON;
    private BufferedWriter fileWriter;

    FileBufferedProcessorService(){  
       createFile();
   }
     ....

    public synchronized void writeToFile(...) {}
    public synchronized void closeFile(...) {}
}
public interface FileProcessorService {

    void writeToFile(...);

    void closeFile(...);

}

Updating an activities field from within a static DateDialogFragment in Android


Tag : java , By : chudq7
Date : March 29 2020, 07:55 AM
this one helps. Hi i had the same problem. This is how i solve it :
Step 1 make your activity implement DatePickerDialog.OnDateSetListener(and remove it from DialogFragment).
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Activity needs to implement this interface
    DatePickerDialog.OnDateSetListener listener = (DatePickerDialog.OnDateSetListener) getActivity();
    DatePickerDialog q = new DatePickerDialog(getActivity(), listener, year, month, day);
    return q;
public class MainActivity extends AppCompatActivity  implements DatePickerDialog.OnDateSetListener {
TextView mChangeDateField;
@Override

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

    mChangeDateField = findViewById(R.id.changeDateField);

}

public void setDateText(View view){
    ClientChooseDateFragment newFragment =
            new ClientChooseDateFragment();
    newFragment.show(getSupportFragmentManager(),
            "datePicker");

}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    mChangeDateField.setText("-- " + year + "--- Month" +month);
}


public static class  ClientChooseDateFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Activity needs to implement this interface
        DatePickerDialog.OnDateSetListener listener = (DatePickerDialog.OnDateSetListener) getActivity();
        DatePickerDialog q = new DatePickerDialog(getActivity(), listener, year, month, day);

        return q;
    }
}}

Creating java singleton with static inner class


Tag : java , By : Mario Tristan
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to fix 'Android SDK not found at this location' error while installing flutter?
  • Android Shared Storage protection issue
  • How can I clone views using java code in Android Studio
  • Hide title and action bar for splash screen and remove White screen in start
  • Why can not hashing key android for facebook
  • Can not create handler inside thread that has not called Looper.prepare()
  • ID of the child views in a custom view are same in Android
  • Android: unable to hide appbar on scrolling
  • Best layout for form-style screen
  • How To create multiple function in Adapter Class Android with Retrofit
  • Can the Android Flow virtual layout handle variable-width Views
  • How to fix "Unable to load class 'dagger.Multibindings'" due to problems with gradle
  • VpnService always-on "not supported by this app"
  • Android Studio - Gradle "Sync now" is not Available
  • How do i get my code to give my current location?
  • Could not initialize class sun.util.calendar.ZoneInfoFile while using Flutter firebase-notifications
  • How to implement circular to check / tick animation in Android?
  • Toolbar shadow not showing
  • Android Sqlite get rows where first character is not an Alphabet
  • why I receive "bad request" in my request by retrofit
  • Intercept NavigationUI.onNavDestinationSelected() to make backstack pop with "inclusive = true"
  • I have a problem with the navigation fragments in my proyect, it sais that I need a defaut nav host atribute but I alrea
  • How can I keep a BottomSheetDialogFragment height to always match_parent?
  • How to reference a timerPicker in android studio using kotlin
  • Firebase Crashlytics - get stacktrace info to display during CrashlyticsListener.crashlyticsDidDetectCrashDuringPrevious
  • Schedule a repeating alarm clock
  • Can't assign values outside onTouchListener
  • Is setContentView() needed to inflate a fragment?
  • Why instead of string method returns Kotlin.Unit?
  • Retrofit slash as Query
  • When does onCreateView() get called in this app?
  • Fragment is reloaded each time after rejecting location permission
  • How to read ViewModel data from sttrings.xml list of items
  • Flutter doctor does not see vs code ,VS code does not detect flutter/bin
  • How to build aar in flutter
  • Is displayMetrix xdpi and ydpi accurate?
  • How to detect doubletap on a View?
  • how to play an encrypted file in Android
  • Android: keytool error on the command line when locating debug.keystore
  • Android paint/canvas issue; drawing smooth curves
  • Problems with starting an activity in onStart
  • Performance of map overlay in conjunction with ItemizedOverlay is very poor
  • How to change android indeterminate ProgressBar color?
  • How do I change the Mobile Country Code (MCC) in the Android Emulator?
  • Stop Progressbar manual scrolling in Android
  • Android: download large file
  • Android : uploaded .apk file to server but cant download that .apk file
  • How do I distribute Android app to beta tester without using Market
  • Android Unable to launch the Inbox from a notification
  • Android: Ringer mode changed to silent doesn't stop the phone vibrating
  • Change the side the text appears on a radio button
  • Check the memory usage anp cpu and power of an application in android
  • What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?
  • Playing Multiple sounds at the same time in Android
  • Android: how to change the time in emulator?
  • close the soft key pad when i click on other views in android
  • Setting the position within a spinner
  • How to play ringtone/alarm sound in Android
  • How to adjust text font size to fit textview
  • "Android 2.x" vs "Google APIs" for Android AVD Setup
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com