Create Button to return to the Android studio application

1

I have 2 Activities: Mainactivity.xml and Messages_predet.xml

I have a button that directs me to the second activity without problem and it will send other activities, but in each one I have to place a button (imagebutton) that will bring me back to the previous activity, to explain myself better:

If I am in the Messages_predet activity, I return to the Main

I have to create an intent?

This is my imagebutton to return to Mainactivity

<ImageButton
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/imageButton"
    android:src="@drawable/back"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true">
</ImageButton>
<!-- how to make this imaggebutton to return to Mainactivity??? -->

And this is what I have in the manifesto

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MensajesPredet"
        android:label="@string/title_activity_mensajes_predet" >
    </activity>
</application>

And this in my MainActivity.java file

package com.globalstar.st300r;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends Activity {

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

    /** Called when the user clicks the Send button */
    public void predefinido(View view) {
        Intent intent = new Intent(this, MensajesPredet.class);
                startActivity(intent);
    
asked by Pablo Vargas Marin 17.08.2017 в 20:49
source

1 answer

2

If you started MensajesPredet from MainActivity in this way:

  Intent intent = new Intent(this, MensajesPredet.class);
  startActivity(intent);

to return to MainActivity just use the finish() method to closePredetMessages and return to MainActivity

  

I have a button that directs me to the second activity without problem and   this same one is going to send other activities, but in each one I must   place a button (imagebutton) that will return me for the activity   previous

If you want to return to the previous Activity just call finish(); from your button.

Otherwise, if the Activity was destroyed by finish(); , for example:

   Intent intent = new Intent(this, MensajesPredet.class);
   startActivity(intent);
   finish();

You need to perform an Intent to reload the Activity that you destroyed, in reality you do not return because it does not exist, what you are doing in this case is to load it again.

  Intent intent = new Intent(this, MainActivity.class);
  startActivity(intent);

How to call the method finish (); from a button, for this use a OnclickListener , example:

ImageButton button= (ImageButton) findViewById(R.id.imageButton); //Obten la referencia mediante el id del boton.
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        finish(); //finaliza Activity.
    }
});
    
answered by 17.08.2017 / 20:58
source