user3001104
user3001104

Reputation: 55

How i can refresh the same activity in static method

My application need to close the activity and open it directly using refresh activity or open new activity and back to the activity ?

public class MainActivity extends Activity {
    static TextView messageBox;
    static String x="";
    static Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         messageBox=(TextView)findViewById(R.id.messageBox);
         check();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


     public static void updateMessageBox(String msg,String from)
        {
            messageBox.append(msg);
            //Get parent phone number from database
            //check parent number with ...
            if(msg.equals("enable wifi"))
            {
                x="yes";
            }
            //context.startActivity(new Intent(context, MainActivity.class));

            Intent i=new Intent(context, p1.class);
            context.startActivity(i);

          }

     private void check()
     {

         if(x.equals("yes"))
         {
             WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
              wifi.setWifiEnabled(true);
         }

     }


}

i'm using open new activity and back to the activity but work crash for application !!

please help me??

Upvotes: 0

Views: 1178

Answers (2)

Sanket Shah
Sanket Shah

Reputation: 4382

Try this,

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

OR

if your application minimum API > 11 then you can call Activity.recreate(); This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to upvote Ralf's answer!)

Upvotes: 0

Hitman
Hitman

Reputation: 598

You can simply use:

finish();
startActivity(getIntent());

to refresh an activity from within itself.

Upvotes: 1

Related Questions