开发者

How to restart an activity after an exception has occured

I have an App, where I load images from a server. Because of this my app leads to 开发者_Python百科Outofmemory Error. I have caught the exception so that my app is now prevented from being force closed. But my app stops loading images in the place where the exception has occurred. So is there a way I could restart my activity after the exception has been caught, so that my memory is freed up and activity loads images from the first once again.

Much needed. Any help is appreciated.


Restarting the current activity all of a sudden is not going to be a great User experience. If possible try clearing Images from memory that are not not shown to the user(so it releases memory that it has occupied).

If still you want to restart the current activity, use the following:

void restartActivity()
{
    CurrentActivity.this.finish();
    Intent mIntent = new Intent(CurrentActivity.this, CurrentActivity.class);
    startActivity(CurrentActivity);
}


You use an Unknown exception handler to get notified when you app crashed , from there on you can load your activity again. But its very important you make sure that this occurs only as many times you see a unicorn. It is best you fix the problem rather than work around it.

Here is a link which will help with UEH : How do I obtain crash-data from my Android application?


I think the issue is in loading images from server which increased the memory causing the application to be in OutOfMemmory Error. You have to avoid your application to take such a huge memory. YOu can use LazyLoading Concept to load images.

Have a look for sample program here

or have a look here also

public class DrawableManager {
    private final Map<String, Drawable> drawableMap;

    public DrawableManager() {
        drawableMap = new HashMap<String, Drawable>();
    }

    public Drawable fetchDrawable(String urlString) {
        if (drawableMap.containsKey(urlString)) {
            return drawableMap.get(urlString);
        }

        Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");
            drawableMap.put(urlString, drawable);
            Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
                    + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
                    + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
            return drawable;
        } catch (MalformedURLException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        } catch (IOException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        }
    }

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
        if (drawableMap.containsKey(urlString)) {
            imageView.setImageDrawable(drawableMap.get(urlString));
        }

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageView.setImageDrawable((Drawable) message.obj);
            }
        };

        Thread thread = new Thread() {
            @Override
            public void run() {
                //TODO : set imageView to a "pending" image
                Drawable drawable = fetchDrawable(urlString);
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            }
        };
        thread.start();
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }

}

Thanks Deepak


You should not be catching Error's in general and OutOfMemoryError in particular. After OOM you can never be sure what is the state of your application.

Instead, you should look into improving your algorithm so OOM does not occur. Describe specific use case (may be in separate question) and somebody can offer you an advice.


Intent  intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);

this will refresh or restart the activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜