Android 中的后退导航没有 ActionBar
Back Navigation in Android Without ActionBar
我有 3 个屏幕,屏幕 1、屏幕 2、屏幕 3。
这就是我正在实现的那种后退导航,
屏幕 3 -> 屏幕 2 -> 屏幕 1。
下面是 屏幕 1
的代码
public class Screen1 extends Activity implements OnClickListener{
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
// This is where the intent starts to display the second Screen
case R.id.button1:
Intent started = new Intent(Screen1.this,Screen2.class);
startActivity(started);
break;
}
}
}
屏幕 2
public class Screen2 extends Activity implements OnTouchListener{
ListView displaylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// Please ignore this part of the code it just displays the list of items
displaylist = (ListView)findViewById(R.id.listView1);
String[] listofitems = {"A","B","C","W"};
ArrayAdapter<String> lists = new ArrayAdapter<String>(Screen2.this, android.R.layout.simple_list_item_1, listofitems);
displaylist.setAdapter(lists);
displaylist.setOnTouchListener(this);
}
// This is where the intent starts to display the Third Screen
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
return true;
}
}
屏幕 3
public class Screen3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Displays some layout
}
}
我目前的问题
我目前的问题是后退导航(当我按下后退按钮时)的顺序如下
屏幕 3 -> 屏幕 3 -> 屏幕 2 -> 屏幕 1
我的问题
为什么按返回键后Screen3又出现了?这是某种错误吗?
来自类似的 question David Wasser comment 说
If your navigation is standard, then 1 starts 2 and the BACK button goes back to 1. 2 starts 3 and the BACK button goes back to 2. You don't need to do anything special to get this
测试设备
Android 模拟器
API Level:19
注意:我不想用 Actionbar 做这个
我认为这是因为您的 onTouch
事件在按下和释放时被触发了两次。
试试这个,或使用其他事件:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
我有 3 个屏幕,屏幕 1、屏幕 2、屏幕 3。
这就是我正在实现的那种后退导航,
屏幕 3 -> 屏幕 2 -> 屏幕 1。
下面是 屏幕 1
的代码public class Screen1 extends Activity implements OnClickListener{
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
// This is where the intent starts to display the second Screen
case R.id.button1:
Intent started = new Intent(Screen1.this,Screen2.class);
startActivity(started);
break;
}
}
}
屏幕 2
public class Screen2 extends Activity implements OnTouchListener{
ListView displaylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// Please ignore this part of the code it just displays the list of items
displaylist = (ListView)findViewById(R.id.listView1);
String[] listofitems = {"A","B","C","W"};
ArrayAdapter<String> lists = new ArrayAdapter<String>(Screen2.this, android.R.layout.simple_list_item_1, listofitems);
displaylist.setAdapter(lists);
displaylist.setOnTouchListener(this);
}
// This is where the intent starts to display the Third Screen
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
return true;
}
}
屏幕 3
public class Screen3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Displays some layout
}
}
我目前的问题
我目前的问题是后退导航(当我按下后退按钮时)的顺序如下
屏幕 3 -> 屏幕 3 -> 屏幕 2 -> 屏幕 1
我的问题
为什么按返回键后Screen3又出现了?这是某种错误吗?
来自类似的 question David Wasser comment 说
If your navigation is standard, then 1 starts 2 and the BACK button goes back to 1. 2 starts 3 and the BACK button goes back to 2. You don't need to do anything special to get this
测试设备
Android 模拟器
API Level:19
注意:我不想用 Actionbar 做这个
我认为这是因为您的 onTouch
事件在按下和释放时被触发了两次。
试试这个,或使用其他事件:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}