0

I want to make switch from one activity to another when the progress bar reached to maximum.

Activity A

public class A extends Activity {
private ProgressBar progressBar;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.mainpage);
    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.main);
    progressBar = (ProgressBar) findViewById(R.id.prog_bar);    

    new AsyncTask<Void, Integer, Integer>(){

        @Override
        protected Integer doInBackground(Void... params) {
            int progressStatus = 0;
            while (progressStatus < 5000) {
                progressStatus++;
                publishProgress(progressStatus);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            return 1;  
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            progressBar.setProgress(values[0]);
        }

        protected void onPostExecute(Integer result) {
            final Intent mainIntent = new Intent(A.this,B.class);
            startActivity(mainIntent);
            finish();
            return;
        }
    }.execute();
}

}

But in this code the onPostExecute() method is not execting...I got stuck with the parameters of aSyncTask also... When I run this code in debug mode it returns from try block

try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }

XML for this class

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:background="@drawable/eggstation"
tools:context=".A" >

<TextView
    android:id="@+id/dutech"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="-5dp"
    android:text="@string/text_data"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#00cccc"
    android:textSize="25sp" />

<TextView
    android:id="@+id/devby"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/dutech"
    android:layout_marginBottom="5dp"
    android:gravity="center"
    android:text="Developed by:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#3399cc"
    android:textSize="28sp" />

<ProgressBar
    android:id="@+id/prog_bar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:max="100"   
    android:layout_alignParentTop="true" />

Rishi Arora
  • 3
  • 1
  • 5

2 Answers2

0

your AsyncTask code has no problem !

problem may be with starting Activity

so try following code in onPostExecute()

final Intent mainIntent = new Intent(A.this,B.class);
startActivity(mainIntent);
finish();
return;
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

Try the following code snippet

 protected void onPostExecute(Integer result) {
        startActivity(new Intent(A.this,B.class));
        return;
    }

Also sleep method take long type as argument, try the following

 Thread.sleep(1000);

Also register your B activity in Manifest.xml like the follwing

<application>
...
...
<activity
        android:name="[package_name].B"
        android:label="@string/class_b" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="[package_name].MainActivity" />
 </activity>
</application
Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45
  • naaaaaa.....not working B is already registered and I made Thread.sleep(1000)...In debug mode when it reach to return statement...It reaches to first line of code(where package is declared)..Its not executing onPostExecute()... – Rishi Arora Jul 29 '13 at 06:59