我想通过按Android Studio上的按钮关闭活动,同时打开另一个活动我试过这个:
public void MainActivity(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
(FirstActivity(MainActivity();)).finish();
}
此代码是在我的项目中的FirstActivity.java中编写的。但不幸的是,没有奏效。
还有其他的做法吗?我做错了吗?
感谢帮帮忙!
必要的信息:
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>
注意:我想在FirstActivity上按下“开始共享”按钮时将用户发送到MainActivity。
答案 0 :(得分:0)
要从第一个活动开始主要活动,它应该是这样的:
public void MainActivity(View v){
startActivity(new Intent(this, MainActivity.class));
}
提示1 :如果您希望用户返回该活动,则无需完成第一个活动,因此如果您完成了我们刚创建的主要活动或按下后退按钮,则只需返回第一个活动而无需创建新活动。
如果要打开主要活动并完成第一个活动,只需添加“finish();”在开始这样的主要活动之后:
public void MainActivity(View v){
startActivity(new Intent(this, MainActivity.class));
finish();
}
提示2 :在这种情况下,你需要开始另一个第一个活动,因为我们完成了它,你不能回到同一个第一个活动。
希望我的答案对你有用:)
答案 1 :(得分:0)
您可以让应用记住用户使用以下方式按下了共享按钮: SharedPreferences 在第一个活动的公共类中添加此行,该行基本上是首选项的名称:
public static final String PREFS_NAME = "PreferencesFile";
在第一个活动的 onCreate 方法中添加代码:
//Get the value of the "FirstBun" key.
SharedPreferences Settings = getSharedPreferences(PREFS_NAME, 0);
boolean FirstRun = Settings.getBoolean("FirstRun", true);
if(FirstRun == false){
startActivity(new Intent(this, MainActivity.class));
finish();
}
按下共享按钮时,请添加以下代码:
public void MainActivity(View v){
//Change the value of "FirstRun" key to 'False' before opening the main activity.
SharedPreferences Settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = Settings.edit();
editor.putBoolean("FirstRun", false);
editor.apply();
//Open the main activity.
startActivity(new Intent(this, MainActivity.class));
finish();
}
注意:首选项是与应用程序文件一起存储的“xml”文件。
如果您在Android设备的AppInfo中清除了该应用的数据,则“FirstRun”键的值将返回默认值“true”。