我有一项活动让我遇到了问题。
问题在于,当我进入活动时,我无法回到之前的状态 活动,除非我按下后退按钮5次。
活动是通过列表输入的,此列表还有其他一些具有相同问题的活动。请注意,并非所有人都有 一样的问题。
这是我的Java代码:
package com.exple.v1;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MoviesMaingenreActivity extends ActionBarActivity {
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list = (ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent;
switch(position)
{
case 1:
intent = new Intent(MoviesMaingenreActivity.this,MovieActionGenreActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(MoviesMaingenreActivity.this,MovieAnimationGenreActivity.class);
startActivity(intent);
break;
case 3:
intent = new Intent(MoviesMaingenreActivity.this,MovieComedyGenreActivity.class);
startActivity(intent);
finish();
case 4:
intent = new Intent(MoviesMaingenreActivity.this,MovieDocumentaryGenreActivity.class);
startActivity(intent);
case 5:
intent = new Intent(MoviesMaingenreActivity.this,MovieHorrorGenreActivity.class);
startActivity(intent);
case 6:
intent = new Intent(MoviesMaingenreActivity.this,MovieDramaGenreActivity.class);
startActivity(intent);
case 7:
intent = new Intent(MoviesMaingenreActivity.this,MovieScifiGenreActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
private void populateListView() {
String[] textz = {"Action","Animation","Komedi","Dokumentär","Skräck","Drama","Sci-fi"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.the_list, textz);
ListView listView = (ListView)findViewById(R.id.listView1);
listView.setAdapter(adapter);
inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header = inflater.inflate(R.layout.header_movies_maingenres,null);
listView.addHeaderView(header,null,false);
}
}
从案例3到案例7确实会出现问题。
为什么这样做?
答案 0 :(得分:4)
您需要在案例编号3,4,5和6中使用break
case 3:
intent = new Intent(MoviesMaingenreActivity.this,MovieComedyGenreActivity.class);
startActivity(intent);
finish();
break; // this is what is missing here...
case 4:
intent = new Intent(MoviesMaingenreActivity.this,MovieDocumentaryGenreActivity.class);
startActivity(intent);
break; // this is what is missing here...
case 5:
intent = new Intent(MoviesMaingenreActivity.this,MovieHorrorGenreActivity.class);
startActivity(intent);
break; // this is what is missing here...
case 6:
intent = new Intent(MoviesMaingenreActivity.this,MovieDramaGenreActivity.class);
startActivity(intent);
break; // this is what is missing here...
答案 1 :(得分:1)
你的问题是你没有打破这个案子,所以意图它在那里遇到了很多麻烦:
case 3:
intent = new Intent(MoviesMaingenreActivity.this,MovieComedyGenreActivity.class);
startActivity(intent);
finish();
//break; missing
case 4:
intent = new Intent(MoviesMaingenreActivity.this,MovieDocumentaryGenreActivity.class);
startActivity(intent);
//break; missing
case 5:
intent = new Intent(MoviesMaingenreActivity.this,MovieHorrorGenreActivity.class);
startActivity(intent);
//break; missing
case 6:
intent = new Intent(MoviesMaingenreActivity.this,MovieDramaGenreActivity.class);
startActivity(intent);
//break; missing
答案 2 :(得分:0)
您需要在每个案例后包含一个break语句。基本上如果3为真,则执行3到7. break语句阻止它在循环中执行代码。 case语句只是告诉它从哪里开始执行代码