0

I have 3 page application, in which the activities change every 10 secs (i.e., Activity1 -> Activity2 -> Activity3 -> Activity1 .......).

I have a progressbar in Activity2 which updates every 1 sec. The problem is, whenever the Activity2 is changing to Activity3 and then coming back to Activity 2 the progressbar is resetting and starting all over from zero.

I would like the progressbar to update continuously for the set time without interruptions. I tried using the services but the result is still the same.

I hope the code below helps in idenfying the issue and would really appreciate if anyone helps me out.

Activity1.java

package com.ossus.SC20;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

    public class frame_6_activity extends Activity {


        Timer timer;

        private View _bg__frame_6;
        private ImageView background_1;
        private TextView text_view_date;
        private ImageView logo_2_1;
        private TextView H2Value;




        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.frame_6);

            startService(new Intent(frame_6_activity.this, service.class));

            Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this));

            if (getIntent().getBooleanExtra("crash", false)) {
                Toast.makeText(this, "App restarted after crash", Toast.LENGTH_SHORT).show();
            }



            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run(){
                    Intent intent = new Intent(frame_6_activity.this, frame_2_activity.class);
                    startActivity(intent);
                    finish();
                }
            }, 10000);

            Calendar calendar = Calendar.getInstance();
            String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

            TextView textViewDate = findViewById(R.id.text_view_date);
            textViewDate.setText(currentDate);


            _bg__frame_6 = (View) findViewById(R.id._bg__frame_6);
            background_1 = (ImageView) findViewById(R.id.background_1);
            text_view_date = (TextView) findViewById(R.id.text_view_date);
            logo_2_1 = (ImageView) findViewById(R.id.logo_2_1);
            H2Value = (TextView) findViewById(R.id.H2Value);


    
    }
}

Activity2.java

package com.ossus.SC20;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.w3c.dom.Text;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

    public class frame_2_activity extends Activity {

        private ProgressBar mProgressBar;
        private TextView mLoadingText;


        private int mProgressStatus = 0;

//      private Handler mHandler = new Handler();

        Handler handler = new Handler();


        Timer timer;

        private View _bg__frame_2;
        private ImageView background_1;
        private TextView text_view_date;
        private ImageView logo_2_1;




    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_2_new);

        startService(new Intent(frame_2_activity.this, service.class));


        mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
        mLoadingText = (TextView) findViewById(R.id.LoadingCompleteTextView);


   
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                startProgress();
            }
        });
        thread.start();



        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run(){
                Intent intent = new Intent(frame_2_activity.this, frame_3_activity.class);
                startActivity(intent);
                finish();
            }
        }, 10000);

        Calendar calendar = Calendar.getInstance();
        String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

        TextView textViewDate = findViewById(R.id.text_view_date);
        textViewDate.setText(currentDate);

        
        _bg__frame_2 = (View) findViewById(R.id._bg__frame_2);
        background_1 = (ImageView) findViewById(R.id.background_1);
        text_view_date = (TextView) findViewById(R.id.text_view_date);
        logo_2_1 = (ImageView) findViewById(R.id.logo_2_1);


        
        //custom code goes here
    
    }

    public void startProgress(){

        for (mProgressStatus = 0; mProgressStatus < 100; mProgressStatus = mProgressStatus + 1){

            try{
                Thread.sleep(1000);
                mProgressBar.setProgress(mProgressStatus);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            handler.post(new Runnable() {
                @Override
                public void run() {
                    mLoadingText.setText(String.valueOf(mProgressStatus + "% Completed"));
                }
            });
        }
    }

}

Service.java

package com.ossus.SC20;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class service extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.ossus.SC20">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SC20"
        tools:targetApi="31">


        <activity
            android:name=".frame_3_activity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:exported="true"  >
        </activity>

        <activity
            android:name=".frame_2_activity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:exported="true"  >
        </activity>

        <activity
            android:name=".frame_6_activity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:exported="true"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

        <service android:name = ".service"/>



    </application>

</manifest> 
Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
  • The service class code you posted doesn't do anything apart from displaying a toast. What did you try that didn't work? Try to save the current progress value in a long living state. This answer describes more how to do that: https://stackoverflow.com/questions/47683170/stateful-long-living-android-service Or even more information about background service on the official page: https://developer.android.com/reference/android/app/Service.html – Omnibyte Jun 20 '22 at 06:37
  • Use `AsyncTask`. It is helpful to work in the background Task to do in Background. Put your `Progressbar` code into this. See here https://stackoverflow.com/a/3028660/16765223 – M DEV Jun 20 '22 at 06:42

0 Answers0