i am new in android .. I want to ask question about Link 2 layouts using buttons. I have 2 xml layout and first layout can link to 2nd layout but 2nd layout can't go back to 1st layout. Please help me.
below are my codes...
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="120dp"
android:text="Link to page 1" />
</RelativeLayout>
page1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page 1 test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to main page" />
</LinearLayout>
MainActivity.java
package com.example.linktest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.page1);
}
});
}}
page1.java
package com.example.linktest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class page1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
Button btn1 =(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
}
});
}}
You're doing it wrong.
To go from MainActivity to page1 (by convention it should be Page1), you should start a new Activity (instead of changing the contentview of the current activity). Then to go back from Page1 to MainActivity, you can programmatically finish() the Activity, or the user can touch Back.
You can either keep your both layout in same layout file and make the show or hide the layout accordingly.
Also another way is you can create two activity which contains two separate layout and load a second layout on click of button using intent and start activity as below
To Go to other activity you can do like this :
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
Normally android deals with that itself.
However you can override OnBackPressed in your second activity and launch an intent that leads to your first activity
Going to other activity you can do like this :
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
For each XML you need to have another Activity.
In Main Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),page1.class);
startActivity(intent);
finish();
}
});
}
Replace this
And in your Page1 Activity replace this
public class page1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
Button btn1 =(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
Hope this will help you
If you want to come back to activity A from activity B with some result,you can use like this
activity B
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
activity A
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
Related
When I wanted to add a function to my button component, I found
a bug in my approach, which is that I have to click the button
once more to achieve the desired effect.
In xml I setup the button like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/Button111"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="button123"
android:text="#string/button" />
</LinearLayout>
My activity file setup like this:
package cn.mr8god.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button123(View view) {
Button button = findViewById(R.id.Button111);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}
You're setting a click listener when you click on the button on the first time. Of course you would need to click it once again.
When you click on the button it will call the button123 function, so you don't need a listener.
Just do it like so:
public void button123(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
i'm trying to start an Activitiy by clicking a Button. If the user clicks the PLAY button in the MenuActivity the GameActivity should be started. But it doesn't work.
public class MenuActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
public void startGameEngine(View view) {
Intent myintent = new Intent(this, GameActivity.class);
startActivity(myintent);
}
Here is the GameActivity class which should be started when the user click on the PLAY-Button.
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Intent intent = getIntent();
}
Here is the menu.xml file which includes the button
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/menubackground_color"
tools:context="com.example.vincenzoauricchio.example.MenuActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="381dp"
android:layout_height="67dp"
android:layout_marginBottom="440dp"
android:layout_marginTop="88dp"
android:fontFamily="#font/vt323"
android:text="#string/logo2"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#android:color/black"
android:textSize="36sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/btnPlay"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginBottom="265dp"
android:layout_marginTop="87dp"
android:fontFamily="#font/vt323"
android:onClick="startGameEngine"
android:text="#string/button_play"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.0" />
Try implementing the on click listner :
public class MenuActivity extends AppCompatActivity implements View.OnClickListener
Try below code snippet :
Button btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(MainActivity.this);
#Override
public void onClick(View view)
{
if(view == btnPlay)
{
Intent intent = new Intent(MenuActivity.this, GameActivity.class);
startActivity(intent);
}
}
Add your GameActivity to manifest.
Try this.
public class MenuActivity extends AppCompatActivity implements View.OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button btnPlay= (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btnPlay:
Intent myintent = new Intent(MenuActivity.this, GameActivity.class);
startActivity(myintent);
break;
}
}
}
Check your Activity in your Manifestfile.xml or not
Add this to your AndroidManifest.xml
<activity android:name=".GameActivity"/>
The clickOn on the layouts.xml are not always a good practice but they work fine.
Instead of setting your on click listener in the xml, set it programatically in the activities on create.
btnPlay.setOnClickListener(new onClickListener{
void onCLick(){
startGameEngine();
}});
and remove View view from startGameEngine()'s input value.
and make sure you add the GameActivity in the manifest.xml
I am creating an Android app - using Android Studio.
The app launches in MainActivity.java which fetches activity_main.xml . On activity_main, the user can select one of 3 buttons. No matter which button they select, it will take them to the SAME layout - primary_layout.xml and the Java class associated with that is PrimaryClass.java .
I have a placeholder in primary_layout. I want this placeholder (id: placeholder) to change according to what button was previously selected.
Eg. If Button 1 (id: button1) is clicked, then the placeholder must say “Button 1 was clicked.”
Or let’s say Button 2 (id: button2) was clicked, then the placeholder must say “Button 2 was clicked”. And the same goes for Button 3.
I have created an intent to open PrimaryClass but I'm not too sure how to code the intent for when the button is clicked. I just don’t know how to change the text of the placeholder, depending on which button the user has clicked. I’ve tried using an ‘if statement’ but it doesn’t seem to work.
Or instead of creating another activity, should I rather create a fragment, and if I should, how would I code the fragment in this particular app?
I have attached images and code to better understand.
And here is my code of my classes and layout files:
MainActivity:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main:
<?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_main"
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.msp.exampleapplication.MainActivity">
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button1"
android:onClick="clickedButton1" />
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"
android:id="#+id/button2"
android:onClick="clickedButton2" />
<Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="#+id/button3"
android:onClick="clickedButton3" />
</RelativeLayout>
PrimaryClass:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
}
}
primary_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="{holder}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:id="#+id/placeholder" />
</LinearLayout>
Your first Activity must look like this:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 1 selected");
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 2 selected");
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 3 selected");
startActivity(intent);
}
});
}
}
Code the second activity as below:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
TextView placeholder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
placeholder=(TextView) findViewById(R.id.placeholder);
placeholder.setText(getIntent().getStringExtra("message"));
}
}
Here is solution of your problem. You have to use Intent to make action in another activity.
Code for MainActivity, create a intent on each button click like below
button_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 1 Clicked");
startActivity(intent);
}
});
Same for button 2 and 3
button_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 2 Clicked");
startActivity(intent);
}
});
button_3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 3 Clicked");
startActivity(intent);
}
});
Now in PrimaryClass use this code inside onCreate Method
String btn_text;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
btn_text = bundle.getString("button_text");
}
Now set text on lable
textView.setText(btn_text);
Hope you understand this, if not let me know, I'll help you.
I am trying to add values to a string and then send it to the next activity. Here is the only relevant part of the code. adding() is the id of the button which appends values to the String msg and sendMessage() sends the msg altogether. When I just send a dummy value, the message is being passed, even when I add something like 1,2,3,4,5 at once instead of adding, it goes. But when I try to add, append and then send, it gives me an "Unfortunately stopped" error.
I tried couple of tweaks, but still the same error. How to fix this?
public void adding(View view){
EditText et1=(EditText) findViewById(R.id.editText1);
msg=msg+","+et1.getText().toString();
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
intent.putExtra("message", msg);
startActivity(intent);
}
EDIT: Activity Code-
package com.example.newlist;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
String msg="";
int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void adding(View view){
EditText et1=(EditText) findViewById(R.id.editText1);
msg=msg+","+et1.getText().toString();
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
intent.putExtra("message", msg);
startActivity(intent);
}
}
Not sure I understand your question correctly
move your findViewById to onCreate
EditText et1;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.editText1);
tv1=(TextView) findViewById(R.id.textView1);
}
in your adding method do it like this
public void adding(View view){
msg = msg + "," + et1.getText().toString();
final String showMessage = msg;
tv1.setText(showMessage);
et1.setText("");
}
in your sendMessage
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
final String msgThatWillSend = msg;
intent.putExtra("message", msgThatWillSend);
startActivity(intent);
}
didn't test yet, but it should be work
try this :
ActivityA.java
public class ActivityA extends Activity{
StringBuilder msg=new StringBuilder();
private EditText et1;
private TextView tv1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
et1=(EditText)findViewById(R.id.et1);
tv1=(TextView)findViewById(R.id.tv1);
}
public void adding(View view){
msg.append(" "+et1.getText().toString());
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
intent.putExtra("message", msg.toString());
startActivity(intent);
}
}
Next.java
public class Next extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(getIntent().getStringExtra("message"));
}
}
test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="12sp" />
<EditText
android:id="#+id/et1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="adding"
android:text="adding"
/>
<Button
android:id="#+id/btnsent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="SendMessage" />
</LinearLayout>
next.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="msg"
android:textSize="25sp" />
</LinearLayout>
I have 2 xml pages, from the main page after clicking the next button it goes to the second page, but when I press on the hardware back button. It does not goes back to the main page.
How do I add the code for the hardware back button?
Testing on Android 4.3.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Page" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Go to Page 1" />
</RelativeLayout>
java class :
package com.example.linktestfyp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.main1);
Toast.makeText(getApplicationContext(), "Button 1 clicked",
Toast.LENGTH_LONG).show();
}
});
}
}
to navigate between activities you should call startActivity(intent); and not changing the contentView of the Same activity.
you should create two activities : Activity1(which will display the main.xml layout) and Activity2(which will display the main1.xml layout) , and while the user is in the Activity2 and press the back button , the Activity1 will be shown again automatically by the system, and the Activity2 will be destroyed.
Example :
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
see this tutorial about how to switch between activities with a demo sample application which you can download at the end of the tutorial .
NB: don't forget to declare your activities in the Manifest file.
This may help you ..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//do your stuff
}
return super.onKeyDown(keyCode, event);
}
You probably want two activities (see below). When you click on the button of MainActivity, the MainActivity1 will start and display the R.layou.main1 layout. If you click the back button, the MainActivity1 will destroy, and the MainActivity will resume (by default, so you don't need to program this)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
}
}
public class MainActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
}
You are not changing activities so when you back press the 1st page is not shown to you,
you should use setcontentview in onbackpressed() method,
then you will be able to show the 1st page otherwise it will always go to the previous activity which is not what you are looking for it seems.