Arda Çebi Arda Çebi - 18 days ago 8
Android Question

Closing other activity and opening a new one by pressing a button: Android Studio

I want to close an activity while opening another one by pressing a button on Android Studio I've tried this:

public void MainActivity(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);

(FirstActivity(MainActivity();)).finish();

}


This code was written in FirstActivity.java in my project. But not worked, unfortunately.

Is there any other way of doing or am I doing it wrong?

Thanks for the helpers out there!

Necessary information:

FirstActivity.java

package com.ardacebi.libra;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class FirstActivity extends AppCompatActivity {

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



public void MainActivity(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);



}



}


MainActivity.java

package com.ardacebi.libra;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}


Activity_First.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ardacebi.libra.FirstActivity">


<ImageView
android:layout_width="200dp"
android:layout_height="150dp"
android:src="@drawable/libralogo"
android:id="@+id/imageView"
android:layout_above="@+id/textView6"
android:layout_centerHorizontal="true" />



<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Libra"
android:textSize="25sp"
android:id="@+id/textView5"
android:gravity="center"
android:layout_marginTop="185dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The file sharing platform"
android:textSize="15sp"
android:id="@+id/textView6"
android:gravity="center"
android:layout_below="@+id/textView5"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Libra, let's you to share your files over a private, secured network to your friends easily"
android:textSize="15sp"
android:id="@+id/textView4"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_below="@+id/textView6"
android:layout_alignParentStart="true" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Sharing"
android:layout_below="@+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:id="@+id/button2"
android:onClick="MainActivity"/>



</RelativeLayout>


Note: I want to send the user to the MainActivity when the Start Sharing button pressed on the FirstActivity.

See Screenshot

Answer

To start the main activity from the first activity, it should be like this :

public void MainActivity(View v){
    startActivity(new Intent(this, MainActivity.class));
}

Tip1 : You don't need to finish the first activity if you want the user to back to it, so if you finish the main activity that we just created or the back button pressed, it will just go back yo the first activity without needing to create a new one.

If you want to open the main activity and finish the first one, just add "finish();" after starting the main activity like this:

public void MainActivity(View v){
    startActivity(new Intent(this, MainActivity.class));
    finish();
}

Tip2 : In this case you need to start another first activity, you can't go back to the same first activity because we finished it.

Hope My Answer Was Useful To You :)