1

I'm trying to restart an Activity in my App, but I have some problems.

When I use recreate();, the activity doesn't completely restart, the EditTexts don't get empty.

I also tried this code:

Intent myIntent = getIntent();
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(myIntent);

BUT:

At the start, my Activity have an EditText and it get auto-focus. I'm using this code to open Keyboard when focus, and close when lost focus:

txtNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
         if (hasFocus) {
             imm.showSoftInput(txtNumber, InputMethodManager.SHOW_IMPLICIT);
         } else {
             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
         }
    }
});

But it doesn't works when Activity starts, so i had to add android:windowSoftInputMode="stateVisible" to the AndroidManifest.xml.

The problem is that: the 2nd code (for restart the activity) ignore this, and don't open the Keyboard at the start.

How can I open the Keyboard at the Activity Start programmatically? Or how can I completely restart an activity using recreate();?

Sorry for bad english, I hope you understand.

Julián Pera
  • 107
  • 5
  • 12
  • 1
    recreate() and call finish(). for older apis (I guess lower 13(?)) startActivity(new Intent(this, getIntent()) and call finish(); and you may put a bundle into the intent, then receive it in onCreate and use it to update/ set up your UI – Martin Pfeffer Jan 20 '16 at 07:55
  • try inside oncreate -- (getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);) from here -- http://stackoverflow.com/questions/5593053/open-soft-keyboard-programmatically – Tasos Jan 20 '16 at 07:57
  • Try to txtNumber.requestFocus() after activity restart at onResume – Allen Chan Jan 20 '16 at 07:58
  • @MartinPfeffer `recreate()` and `finish()` just close the activity. – Julián Pera Jan 20 '16 at 07:59
  • @JuliánPera sorry, my bad... recreate does invoke finish() already.. – Martin Pfeffer Jan 20 '16 at 08:03
  • first start activtiy and then finish in your 2nd code – Vivek Mishra Jan 20 '16 at 08:15
  • @MartinPfeffer With `recreate()`, the Activity's EditTexts don't get empty. @Tasos Same result that using `android:windowSoftInputMode="stateVisible"`. @AllenChan Nothing, i also tried turning on the focuseable attribute in the layout. – Julián Pera Jan 20 '16 at 08:15
  • @VivekMishra Not found. Keyboard still not open. – Julián Pera Jan 20 '16 at 08:22
  • @TargetApi(Build.VERSION_CODES.CUPCAKE) public void changeKeyboardState() { InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } – Martin Pfeffer Jan 20 '16 at 09:02
  • Possible duplicate of [How do I restart an Android Activity](http://stackoverflow.com/questions/1397361/how-do-i-restart-an-android-activity) – Jade May 20 '17 at 16:38

3 Answers3

2
 Try this:
Intent intent = getIntent();
finish();
startActivity(intent);
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

add focusable to your EditText and call requestFocus() when you need to show the the keyboard.

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • I guess that EditText is focuseable by default. Anyway, i added `focusable`/`focusableInTouchMode` and put `requestFocus()` in `onResume`, but nothing. Keyboard doesn't show up. – Julián Pera Jan 20 '16 at 09:45
-1

What about this?:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108