-4

Here is the code i used for group bar_chart using MPAndroid library version 3. I've tried group bar chart in lower version and it works fine but the BarData constructor is changed in latest version. And this code doesn't works. There is no crashes and error logs, still graph is loading and no data shows in it. Help me to find ma flaws

        List<BarEntry> entriesGroup1 = new ArrayList<>();
        List<BarEntry> entriesGroup2 = new ArrayList<>();
        List<BarEntry> entriesGroup3 = new ArrayList<>();

        entriesGroup1.add(new BarEntry(0, 8f));
        entriesGroup1.add(new BarEntry(1, 2f));
        entriesGroup1.add(new BarEntry(2, 5f));
        entriesGroup1.add(new BarEntry(3, 20f));
        entriesGroup1.add(new BarEntry(4, 15f));
        entriesGroup1.add(new BarEntry(5, 19f));

        entriesGroup2.add(new BarEntry(0, 6f));
        entriesGroup2.add(new BarEntry(1, 10f));
        entriesGroup2.add(new BarEntry(2, 5f));
        entriesGroup2.add(new BarEntry(3, 25f));
        entriesGroup2.add(new BarEntry(4, 4f));
        entriesGroup2.add(new BarEntry(5, 17f));

        entriesGroup3.add(new BarEntry(0, 9f));
        entriesGroup3.add(new BarEntry(1, 1f));
        entriesGroup3.add(new BarEntry(2, 15f));
        entriesGroup3.add(new BarEntry(3, 13f));
        entriesGroup3.add(new BarEntry(4, 40f));
        entriesGroup3.add(new BarEntry(5, 25f));

        BarDataSet set1 = new BarDataSet(entriesGroup1, "Group 1");
        BarDataSet set2 = new BarDataSet(entriesGroup2, "Group 2");
        BarDataSet set3 = new BarDataSet(entriesGroup3, "Group 3");

        final ArrayList<String> labels = new ArrayList<String>();
        labels.add("2016");
        labels.add("2015");
        labels.add("2014");
        labels.add("2013");
        labels.add("2012");
        labels.add("2011");
        IAxisValueFormatter formatter = new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {

                if((int) value < 0 || (int) value >= labels.size()){
                    return "";
                }else{
                    return labels.get((int) value);
                }
            }

            // we don't draw numbers, so no decimal digits needed
            @Override
            public int getDecimalDigits() {  return 0; }
        };

        set1.setColor(Color.parseColor("#cd5080"));
        set2.setColor(Color.parseColor("#0d5080"));
        set3.setColor(Color.parseColor("#fc5080"));;

        float groupSpace = 0.06f;
        float barSpace = 0.02f; // x2 dataset
        float barWidth = 0.45f; // x2 dataset
// (0.02 + 0.45) * 2 + 0.06 = 1.00 -> interval per "group"

        XAxis xAxis = barChart.getXAxis();
        xAxis.setCenterAxisLabels(true);
        xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
        xAxis.setValueFormatter(formatter);

        BarData data = new BarData(set1, set2, set3);
        data.setBarWidth(barWidth); // set the width of each bar
        barChart.setData(data);
        barChart.groupBars(2016, groupSpace, barSpace); 
        barChart.invalidate(); // refresh

        barChart.animateY(5000);

Nb: I'm edited my current question since i'm banned to ask new questions. But its important for me. Thanks everyone.

SARATH V
  • 500
  • 1
  • 7
  • 33
  • @Nilu i've seen this answer. my project has almost 87 activity pages, so does it required to edit in all activies to include onPause and onResume? Or any alternate way? – SARATH V Dec 07 '17 at 08:33
  • check this https://tips.androidhive.info/2015/04/android-how-to-check-if-the-app-is-in-background-or-foreground/ – AskNilesh Dec 07 '17 at 08:37
  • 2
    `... almost 87 activity pages ...` Holy crap! Why so many Activities, whereas you could use Fragments and only one (or a few) Activity(es)?! – Phantômaxx Dec 07 '17 at 08:42
  • @NoiseGenerator that much views are in this app – SARATH V Dec 07 '17 at 08:45
  • 2
    `that much views are in this app` ... one Activity per View? That would be really crazy! – Phantômaxx Dec 07 '17 at 09:01
  • @NoiseGenerator sounds crazy too for me when i first met with this project. already completed client project by another development team. doing bug fix on that. now you understand why i required these kind of questions. – SARATH V Dec 07 '17 at 10:59
  • You should tell your customer that since the app was poorly written (to be kind), it has to be **totally reengineered**, from A to Z. – Phantômaxx Dec 07 '17 at 11:06
  • @NoiseGenerator , thats what i'm thinking right now. Can you help me on another question? https://stackoverflow.com/questions/47693717/how-to-open-inactive-app-activity-from-notification-click?noredirect=1#comment82346608_47693717 – SARATH V Dec 07 '17 at 11:16
  • Throw in the question, let's see who can help you better. – Phantômaxx Dec 07 '17 at 11:18

4 Answers4

1

Create a Application class and add counter of foreground activities, if counter is 0 means your activity is in background

class MyApp extends Application{
 public int foregroundActivities  = 0 ;

 @override
 public void onCreate(){
     super.onCreate();
     this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

                @Override
                public void onActivityStarted(Activity activity) {
                    foregroundActivities++;
                }

                @Override
                public void onActivityStopped(Activity activity) {
                    foregroundActivities--;
                }
        }
   }
}

EDIT

To answer your question, Application class is singleton to Lifecycle so you can register Activity Life cycle call backs as above which will be triggered by all your 87 activities.

Suppose, If any of your 87 activities is started, it will call onActivityStarted() method, where the count of foreground activity will become 1, if activity is closes, it will call onActivityStopped() which will decrement count of foreground activities back to 0.

So you can detect whether application is in foreground or background based on counter,

if(applicationInstance.foregroundActivities > 0){
    //App is in foreground
 }else{
    //App is in background
 }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • I've already has an application class. in oncreate, it looks new to me. can you explain the flow of this code. – SARATH V Dec 07 '17 at 08:49
  • I've added description stating the function of activity lifecycle call back in application class – Rajan Kali Dec 07 '17 at 09:57
  • ok thanks. one more doubt about it, what if when i'm finishes the last activity and return to previous activity? does it mark as count 0 or 1? – SARATH V Dec 07 '17 at 10:02
  • as long as any activity is on screen, count will not be 0, if count = 0, it means all the activities are in background – Rajan Kali Dec 07 '17 at 10:04
  • thanks @rajanks. i'll reply back after implented on ma app – SARATH V Dec 07 '17 at 10:06
  • thanks for your valuable time. i've changed question since i'm banned from asking new question. And its important doubt for me now, hope you can understand. – SARATH V Dec 15 '17 at 07:35
1

This is not my answer, i have seen this ques before and impleneted in my app it worked like a charm. Couldn't find the link to the original answer so i am posting the code that i used. You can use ActivityLifecycleCallbacks :

public class AppTransitionStatus implements Application.ActivityLifecycleCallbacks {

private static final long MAX_BACKGROUND_TIME = 2000;
private Timer timer;
private TimerTask task;
private boolean isBackground;

private final Transition transition;

private void goToForeground(Activity iActivity) {
    transition.appToForeground(iActivity);
}

private void goToBackground() {
    transition.appToBackground();
}

private void stopTimer() {
    if (task != null) task.cancel();
    if (timer != null) timer.cancel();
}

public AppTransitionStatus(@NonNull Transition transition) {
    this.transition = transition;
}

@Override
public void onActivityCreated(Activity activity, Bundle bundle) {

}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {
    if (isBackground) {
        goToForeground(activity);
    }
    stopTimer();
    isBackground = false;
}

@Override
public void onActivityPaused(Activity activity) {
    timer = new Timer();
    task = new TimerTask() {
        public void run() {
            isBackground = true;
            goToBackground();
        }
    };
    timer.schedule(task, MAX_BACKGROUND_TIME);
}

@Override
public void onActivityStopped(Activity activity) {

}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

}

@Override
public void onActivityDestroyed(Activity activity) {

}

public interface Transition {
    void appToForeground(Activity iActivity);

    void appToBackground();
}
}

And then you can register this in your application class

 registerActivityLifecycleCallbacks(new AppTransitionStatus(new AppTransitionStatus.Transition() {
        @Override
        public void appToForeground(Activity iActivity) {

            Log.d("Transition", "App in Foreground");

        }

        @Override
        public void appToBackground() {
            Log.d("Transition", "App in Background");
        }
    }));
  • thanks for your valuable time. i've changed question since i'm banned from asking new question. And its important doubt for me now, hope you can understand. – SARATH V Dec 15 '17 at 07:35
1

Use blow code :

    public boolean isActivityRunning(Context context) {
   boolean isActivityFound = false;
    ActivityManager activityManager = (ActivityManager)context.getSystemService (Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (int i = 0; i < activitys.size(); i++) {
        if (activitys.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.example.testapp/com.example.testapp.Your_Activity_Name}")) {
            isActivityFound = true;
        }
    }
    return isActivityFound;
}

here com.example.testapp is your package name

need to add the permission to your manifest..

<uses-permission  android:name="android.permission.GET_TASKS"/>

it returns u activity running or not by using name on it and also change as your requirement .

UPDATE

public static ArrayList<ActivityInfo> getAllRunningActivities(Context context) {
try {
    PackageInfo pi = context.getPackageManager().getPackageInfo(
            context.getPackageName(), PackageManager.GET_ACTIVITIES);

    return new ArrayList<>(Arrays.asList(pi.activities));

} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
    return null;
}
}

This method give u list of all running activitys in your project

0

You can use the activity hasWindowFocus() boolean method. Then if it has focus you display a pop-up from the activity, if not, you just display the notification.

Ameer Taweel
  • 949
  • 1
  • 11
  • 37