Reputation: 37
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);
}
});
}}
Upvotes: 0
Views: 7840
Reputation: 576
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
}
}
}
Upvotes: 0
Reputation: 255
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
Upvotes: 0
Reputation: 23638
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);
Upvotes: 1
Reputation: 22316
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.
Upvotes: 1
Reputation: 850
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.
Upvotes: 0