java - Using methods across all activities in android -


i know there questions on global methods , variables in android i'm running problems static methods due less experience objectoriented programming. here want have:

i writing app counts points user can earn things does. because of want call method addpoints different activities , services. method should set points textview in main activity , other things.

i realized adding static variable

static int spoints; 

in mainactivity, use "global" variable in each activity. however, addpoints method have problems. if use non-static method, have create instance of mainactivity in other activities, not nice , changing values of instance not have effect on actual mainactivity.

if use static function works fine long don't want use non-static methods in example:

public static void addpoints(context context, int points){     int levelbefore, levelafter;      levelbefore = getlevelfrompoints(spoints);     spoints = spoints + points;     levelafter = getlevelfrompoints(spoints);      if(levelbefore!=levelafter){         string rank = getrankfromlevel(levelafter);         leveltextview.settext("lvl. " + string.valueof(levelafter));         toast.maketext(context, "congrats! reached next level!", toast.length_long).show();     } } 

here can't use leveltextview.settext , run problem in many other cases. moreover, i've read using static methods not good, anyway.

so correct way creating instance of mainactivity each time , call addpoints on has return new number of points? or there way (i hope so, because both above ways seem not satisfying).

a. static methods can safely used in case work can not accomplished shardpreferences , requires use of same code @ multiple classes in case.
b. first create interface pass updated rank respective activities or classes

public interface scoreupdater {     void updatescore (string rank); } 

c. implement in activities required use, mainactivity in case

public class mainactivity extends appcompatactivity implements scoreupdater{      //     //other methods , codes    //     @override     public void updatescore(string rank) {         runonuithread(new runnable() {             @override             public void run() {                 leveltextview.settext("lvl. " + string.valueof(levelafter));                 toast.maketext(mainactivity.this.getapplicationcontext(), "congrats! reached next level!", toast.length_long).show();             }         });     } } 

d. implement static methods. not sure have declared few variables. method below on guess work.

public static void addpoints(context context, int points){         //not sure declaring spoints         int levelbefore, levelafter;         levelbefore = getlevelfrompoints(spoints);         spoints = spoints + points;         levelafter = getlevelfrompoints(spoints);          if(levelbefore!=levelafter){             string rank = getrankfromlevel(levelafter);             if(context instanceof scoreupdater){                 ((scoreupdater)context).updatescore(rank);             }         }     }      private static int getlevelfrompoints(int points){         //your operations         return points;     } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -