Code for
FirstActivity ---> SecondActivity ---> ThirdActivity
backPress: Thied ---> Second ---> First
When you click the GoToMainActivity button in ThiredActivity it will directly go to MainActivity by finishing the SecondActivity also
MainActivity ;
--------------
public class MainActivity extends Activity {
private Button next;
public static boolean back = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = (Button) findViewById(R.id.next);
next.setText("First");
}
public void nextActivity(View view){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
SecondActivity :
----------------
public class SecondActivity extends Activity {
private Button next;
public static boolean back = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = (Button) findViewById(R.id.next);
next.setText("Second");
}
public void nextActivity(View view){
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
}
@Override
public void onResume(){
super.onResume();
/*
* check the cundition wether user click the recomencer button if click colose this activity else resume the activity
*/
if(back!= false){
back();
}
}
public void GoBack(View view){
back();
}
/*
* this ethode is usefull to finish this activity when click recomencer button click
*/
public void back(){
finish();
back = false;
}
}
ThirdActivity ;
--------------
public class ThirdActivity extends Activity {
private Button next;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = (Button) findViewById(R.id.next);
next.setText("Go to MainActivity");
}
public void nextActivity(View view){
SecondActivity.back = true;
finish();
}
}
main.xml:
----------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:onClick="nextActivity"
android:text="first" />
</RelativeLayout>
No comments:
Post a Comment