Do not go back to a previous activity

2

The fact is that I want that when the application is executed for the first time, the first activity of PresentacionActivity is shown and that when the user press continue button go to the next activity, start the MainActivity. After this, that PresentacionActivity will no longer appear again.

What I want is the activity of PresentacionActivity show only one ves.The case is that I want that when the application is executed for the first time, it shows the first activity of PresentacionActivity and that when the user press continue button go to the next activity, the MainActivity starts. After this, that PresentacionActivity will no longer appear again.

What I want is the activity of PresentacionActivity to show only one time and not to show it again. I used the finish and other methods but at the moment of closing the application it completely turns to PresentacionActivity .... someone can help me

    
asked by Geyser 17.09.2018 в 19:09
source

6 answers

1

Execute the finish() method when navigating to the activity:

startActivity....
finish();//...

This method removes the invoking activity from the navigation stack.

Important : Always remember to run the method finish () after of startActivity

    
answered by 17.09.2018 в 19:33
0

Simply call finish() after starting MainActitiy , this to end the PresentacionActivity and do not show it again, because if it keeps appearing when closing MainActivity , this is an example:

 //Abre MainActivity desde PresentacionActivity.
 Intent intent = new Intent(PresentacionActivity.this, MainActivity.class);
 startActivity(intent);
 //Finaliza PresentacionActitiy.
  finish();
    
answered by 17.09.2018 в 19:55
0

Why do not you better use another activity for your presentation and leave MainActivity for the normal use of your app.

When you load for the first time MainActivity immediately launches your Presentation Activity from onStart , and when they finish viewing your presentation, it saves a value in SharedPreference to then know if you should show the presentation again or No.

@Override
protected void onStart() {
    super.onStart();

    if(!presentacionVista){
        Intent intent = new Intent(this, presentacionActivity.class);
        startActivity(intent);
    }
}
    
answered by 17.09.2018 в 21:48
0

You can do the following.

In the Manifest add this to the activity tag that will be the Welcome screen.

        android:noHistory="true"

This helps if the user presses the back button will not return to the Welcome screen, it would be like this.

       <activity
        android:name="com.example.example.example.Bienvenida"
        android:label="@string/app_name"
        android:noHistory="true"
        android:theme="@style/AppTheme.NoActionBar"
        />

When executing the application for the first time and pressing the Next button, what we will do is create a Preference. We will create a method.

public void CargarPreferencias(Activity contex, String inicio){
    SharedPreferences Preferencias = contex.getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = Preferencias.edit();
    editor.putString("inicio",inicio);
    editor.apply();
}

The summary form code would remain so.Activity Welcome.

public class Bienvenida extends AppCompatActivity {

private Button acceder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);



    acceder = (Button) findViewById(R.id.procesar_login);
    assert acceder != null;
    acceder.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            /*
            Creamos la Preferencia y guardamos el valor del Key inicio=Inciado.
             */
            CargarPreferencias(Bienvenida.this,"Inciado");
        }
    });



  }


  /*
   Creamos la Preferencia y guardamos el valor del Key inicio
  */
 public  void CargarPreferencias(Activity contex, String inicio){
    SharedPreferences Preferencias = contex.getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = Preferencias.edit();
    editor.putString("inicio",inicio);
    editor.apply();
}

}

In our Activity Main it would be like this.

public class ActivityMain extends AppCompatActivity {

private Button acceder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

}

 /*Este Metodo es un ciclo de vida de un Activity y se ejecuta justo antes de crear la vista*/

@Override
public void onResume(){
    super.onResume();
    /*
    Verificamos si el user ya vio la Bienvenida si ya la vio no hacemos nada y se muestra el activity Main.
    Pero si es la primera vez Llamamos al Activity Bienvenida.
     */
    /*si es distinto a Inciado Llamamos a la Presentacion*/
    if(!getPreferencia(ActivityMain.this).equals("Inciado")){
        Intent acceso = new Intent(this ,Bienvenida.class);
        startActivity(acceso);
    }

}

    /*
    Funcion que retorna el valor de la preferencia con key inicio
     */

public  String getPreferencia(Activity contex){
    SharedPreferences prefs =contex.getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
    return  prefs.getString("inicio","");
}








}
    
answered by 22.09.2018 в 03:36
-1

In the manifest, change so that your MainActivity is the initializer of your application.

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="nosensor"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

And remove the "Intent-filter" from the activity of PresentacionActivity

<activity
            android:name=".PresentacionActivity"
            android:label="@string/title_activity_configuracion"
            android:parentActivityName=".actividades.LoginActivity"
            android:screenOrientation="nosensor"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>

In the onCreate method of the MainActivity activity, you have the following code:

 Intent intent = new Intent(context, PresentacionActivity.class);    
 startActivity(intent, requestCode)...

If you open another activity from the MainActivity you will always return to MainActivity and as long as the method onCreate of MainActivity is not launched again PresentacionActivity

To prevent it from launching again PresentacionActivity review the life cycles of an app Android as well as changes in activities that execute onCreate .

    
answered by 17.09.2018 в 19:53
-1

If I understood correctly, you want it to only open the first time the app is run and never again, if so, the solution would be like this:

In the manifest we set the MainActivity as Main and as Launcher to be the first to run and from there decide if it is the first time and corresponds to open the PresentacionActivity or not:

<activity android:name=".MainActivity">
   <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER"/>
   </intent-filter>
</activity>
<activity android:name=".PresentacionActivity/>

In the MainActivity we will use the SharedPreference to save a bool and thus to know if that screen was ever shown

@Override
protected void onCreate(){
   ...
   SharedPreferences prefs = getSharedPreferences("MyPreferences",Context.MODE_PRIVATE);
   if(!prefs.getBoolean("isPresentacionShowed",false)){
      Intent intent = new Intent(this, PresentacionActivity.class);
      startActivity(intent);
   }
}

And in PresentacionActivity you keep true in the sharedPreferences

@Override
protected void onCreate(){
   ...
   SharedPreferences prefs = getSharedPreferences("MyPreferences",Context.MODE_PRIVATE);
   SharedPreferences.Editor editor = prefs.edit();
   editor.putBoolean("isPresentacionShowed",true);
   editor.apply();
}

Anyway, I always recommend having an activity Splash that takes care of this type of redirects, because of having a more complex life cycle the app could be cumbersome to handle it in this way.

    
answered by 18.09.2018 в 00:00