4

The app I'm coding checks wether there is a special ZIP-File in a Directory under /sdcard and starts to download and unzip it if not. The download and unzip works finde, even with subdirectories. But I need to restart the App when it's done - and that does not work.

At first I have a special Activity "PreMainActivity.java" just for restart purposal:

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

public class PreMainActivity extends Activity
{

/**
 * 
 */
public static Boolean   ENABLE_RESTART  = false;

@Override
public void onCreate(final Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    PreMainActivity.ENABLE_RESTART = true;
    restartMain();
}

@Override
public void onRestart()
{

    super.onRestart();
    restartMain();
}

/**
 * 
 */
public void restartMain()
{

    if (PreMainActivity.ENABLE_RESTART == true)
    {
        final Intent mainIntent = new Intent(this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(mainIntent);
        finish();
    }
    else
    {
        finish();
    }
    PreMainActivity.ENABLE_RESTART = false;
}
}

then I got some code within the DownloadFile.java

@Override
protected void onPostExecute(final String result)
{

    MainActivity.mProgressDialogDownload.dismiss();
    PreMainActivity.ENABLE_RESTART = true;
    final Intent i = new Intent(MainActivity.this, PreMainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(i);
}

As far as I have researched I need to pass the context of my MainActivity to the DownloadFile.java - but I still have no clue how. Can anyone gibe me a hint how to pass the context to a AsyncTask in a separate file within the same package? Or any other hint how to restart the whole app after a AsyncTask has finished ?

Donny
  • 95
  • 10
  • why do you need to restart the App when it's done ? – dorjeduck Oct 05 '12 at 16:57
  • Because the Code I am downloading is a 21MB-JavaScriptCode-package which I need within webview for the app to work - so the download starts after the first launch of the app. But after the download finished I need to reload the WebView. But a simple webview.reload() does not work - a reload of the activity either - and a restart of the whole app works within a second. Maybe not the best way - but user of apps often face apps which load some content after first launch and which have to be reloaded after that. So I decided to go this way :-) – Donny Oct 05 '12 at 18:23
  • ok - i see you got an answer which works well for you so no need to elaborate further from my side ;-) – dorjeduck Oct 05 '12 at 18:38

2 Answers2

1

You will need to Create a constructor of the AsyncTask to pass Current Activity Context as:

   public Context ctx;

    public Your_AsyncTask_Class_Name (Context context){
        super();
        this.ctx=context;

    }
  ......
@Override
protected void onPostExecute(final String result)
{

    MainActivity.mProgressDialogDownload.dismiss();
    PreMainActivity.ENABLE_RESTART = true;
    final Intent i = new Intent(ctx, PreMainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(i);
}

and from Activity you can pass context as:

AsyncTask_Class_Name asyktaskobj=new AsyncTask_Class_Name(this);
asyktaskobj.execute(); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

Just restart your main.activity like this:

Intent intent = getIntent(); 
finish(); 
startActivity(intent);

See question: How do I restart an Android Activity

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Unfortunately this code snipplet works from within the MainActivity - but not within a method below an AsyncTask Class. Anyway thank you for your Post - I will keep it in mind, maybe I need this on annother project :-) – Donny Oct 05 '12 at 18:17