Search code examples
androidandroid-activitydrawredrawonresume

re-draw an activity in Android


my problem seems simple but I can not manage to find an answer here.

I have different activities in one application. Activities start each other. I have a "main-screen" that shows all the different available levels, user clicks on a level and that activity is lunched, when user finishes that level, he can press the back-button to go to the main-screen and start another level. When back-button is pressed I would like to re-draw the "main-screen" again, since this time the thumb for the completed level will be different to show that it is completed.

So how do I run a re-draw on a activity upon back-button pressed? (I suppose I should put some code in the Activity.onResume method) (I use SharedPreferences to save the state of finished levels)

The main-screen activity is basically this:

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);

SharedPreferences settings = getSharedPreferences((getResources().getString(R.string.PREFS_HI)),0); /**/ GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this,(settings.getBoolean("level_1_finished", false)))); /**/ gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //Open the map which was clicked on, if there is one if(position+1 > 1){ Toast.makeText(maps.this, "Level " + (position+1) + " is not yet available!", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(maps.this, "Opening Level " + (position+1), Toast.LENGTH_SHORT).show(); Intent myIntent = new Intent(v.getContext(), Tutorial2D.class); startActivity(myIntent); } } }); }

ImageAdapter then uses the boolean true/false to choose the source of the image, either the unfinished thumb or the finished level thumb.


Solution

  • First put your GridView objet as a class member. Then override the onResume() method in which you can re-set the grid view adapter based on what the user did.

    private GridView gridview;
    @Override
    protected void onResume() {
        super.onResume();
        gridview.setAdapter(/* your new ImageAdapter here */);
    }