Reputation: 83
I have one activity which has 2 button clicking on which takes to second and third activity. Second activity has 2 buttons "return to main activity" and "click to third activity". When I click on to "return main activity button" it isn't returning to main activity screen But when I click on "go to 3rd activity" button it is going to third activity. Can someone help me why my "return main activity button" isn't working?
here is my code
main activity
{
package com.example.aditya.activitywithinactivity;
import android.content.Intent;
import android.os.Bundle;
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 android.widget.Button;
import android.view.View.OnClickListener;
import static com.example.aditya.activitywithinactivity.R.id.start;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bttn1 = (Button) findViewById(R.id.button1);
bttn1.setOnClickListener(MainActivity.this);
Button bttn2 = (Button) findViewById(R.id.button2);
bttn2.setOnClickListener(MainActivity.this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
case R.id.button2: {
Intent intent1 = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(intent1);
}
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setContentView(R.layout.activity_main);
};
}
SecondActivity.java
package com.example.aditya.activitywithinactivity;
import android.content.Intent;
import android.os.Bundle;
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.widget.Button;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button bttn3 = (Button) findViewById(R.id.button3);
bttn3.setOnClickListener(SecondActivity.this);
Button bttn4 = (Button) findViewById(R.id.button4);
bttn4.setOnClickListener(SecondActivity.this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button3: {
Intent intent2 = new Intent(this, MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent2);
}
case R.id.button4: {
Intent intent3 = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent3);
}
}
};
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClicked"
android:text="Click to go to Secondary Activity" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClicked"
android:text="Click to go to Third Activity" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
activity_second
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myText2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClicked"
android:text="Return to Main Activity"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClicked"
android:text="Click to go to Third Activity"/>
</LinearLayout>
Upvotes: 1
Views: 1346
Reputation: 551
There is no break; statement in your switch. It just continues to the next case step and therefore next activity. Also you can simply call finish() to end your current activity and go back - this will work better for your backstack. Anyway add break; after each case. Please check this Oracle tutorial about Switch statement.
Upvotes: -1
Reputation: 2155
The two lines code is in your source:
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
I want to know what do you want to do between two activities. The two sources follow can do it, but they are different.
//Source code 1:
Intent intent2 = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent2);
//Source code 2:
onBackPressed(); // or finish();
If use source code 1, the activities task will create another MainActivity object and put it to the top of task.
MainActivity@obj1 | SecondActivity@obj1 ==>
MainActivity@obj1 | SecondActivity@obj1 | MainActivity@obj2
If use source code 2, the activities tasks will remove SecondActivity object so that the MainActivity object at the top.
MainActivity@obj1 | SecondActivity@obj1 ==>
MainActivity@obj1
Upvotes: 0
Reputation: 148
change in MainActivity
Intent i= new Intent(MainActivity.this, SecondActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
Upvotes: 1
Reputation: 3347
try this:
public void onClick(View v) {
switch (v.getId()) {
case R.id.button3: {
Intent intent2 = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent2);
break;
}
case R.id.button4: {
Intent intent3 = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent3);
break;
}
}
};
Upvotes: 2
Reputation: 5370
You can call method onBackPressed();
when return MainActivity
button is clicked like this in SecondActivity.java
and use break
in switch
statement
public void onClick(View v) {
switch (v.getId()) {
case R.id.button3:
onBackPressed();
break;
case R.id.button4:
Intent intent3 = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent3);
break;
}
};
Upvotes: 1