Home > Software engineering >  How to send data from class to activity on Android
How to send data from class to activity on Android

Time:04-04

I have a problem with send data from class to activity. I have class (not activity) with static method:

public static void printUsageStats(List<UsageStats> usageStatsList){

    long totalTime = 0;

    for (UsageStats u : usageStatsList) {
        if ((u.getTotalTimeInForeground() > 0) && u.getPackageName().equals("com.facebook.katana")) {
            Log.d(TAG, "Pkg: "   u.getPackageName()   "\t"   "ForegroundTime: "
                      u.getTotalTimeInForeground());

            totalTime = totalTime   u.getTotalTimeInForeground();

        }
    }
    System.out.println(totalTime);

}

And I need send variable totalTime to mainactivity to set text on textview with this variable after click button.

Here is a second method which I call in mainactivity:

public static void printCurrentUsageStatus(Context context){
    printUsageStats(getUsageStatsList(context));

}

And in onCreate mainactivity I call it:

statsBtn = (Button) findViewById(R.id.stats_btn);
    statsBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UStats.printCurrentUsageStatus(MainActivity2.this);

        }
    });

But with that's way I can't set text in textview.

CodePudding user response:

As for current implementation you can just call static method from Activity and get returned value from this method, as a easiest approach. Also you can make a DI (dagger/koin/hilt), make a repository with a LiveData and subscribe to this LiveData from Activity to get new value.

CodePudding user response:

Method 1 - Return the value

The most straightforward way to do that would be to just return the value you want to display from the method, like this

// change return type from 'void' to 'long' and add a 'return'
public static long printUsageStats(List<UsageStats> usageStatsList){

    // your existing code...

    return totalTime;
}

public static long printCurrentUsageStatus(Context context){
    return printUsageStats(getUsageStatsList(context));
}

and then you can return the time value and set it within your activity

textFld = (TextView)findViewById(R.id.my_text);
statsBtn = (Button) findViewById(R.id.stats_btn);

statsBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        long time = UStats.printCurrentUsageStatus(MainActivity2.this);
        textFld.setText(String.valueOf(time));
    }
});

Method 2 - Pass the TextView as Argument

Alternately you could pass the TextView into the function as an argument, then you can set the value in the static function.

public static void printUsageStats(List<UsageStats> usageStatsList, TextView tv){

    // your existing code...

    tv.setText(String.valueOf(totalTime));
}

public static void printCurrentUsageStatus(Context context, TextView tv){
    printUsageStats(getUsageStatsList(context), tv);
}

and call it like this

textFld = (TextView)findViewById(R.id.my_text);
statsBtn = (Button) findViewById(R.id.stats_btn);

statsBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        UStats.printCurrentUsageStatus(MainActivity2.this, textFld);
    }
});
  • Related