gpt4 book ai didi

java - 相同的 Activity 堆叠

转载 作者:行者123 更新时间:2023-11-30 02:32:16 27 4
gpt4 key购买 nike

我有一个 Activity 应该自行完成并关闭应用程序。现在,在某些情况下,根据用户导航到 Activity 的方式而变化, Activity 正在堆叠。当 Activity 堆积时,然后调用 finish()android.os.Process.killProcess(android.os.Process.myPid()); 或两者一起调用是只是再次出现相同的 Activity 。

list :

<activity
android:name="newActivities.HomeActivity"
android:label="@string/title_activity_home"
android:screenOrientation="portrait">
</activity>

Activity :

public class HomeActivity extends Activity {

private EditText studentNameEdittext;
private Button startYourStoryButton, loginButton, navCollegesButton, settingsButton, search_friends_button, browseStoriesButton;
private TextView textView1;


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
// finish();
// saveInPreference("loginStatus", "");
// }
// Set up the action bar
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009945")));
bar.setTitle("Study Story");
bar.setIcon(R.drawable.statusbar_icon);
int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.white_colour));
yourTextView.setTypeface(TypeFaceController.titleFace(HomeActivity.this));



// studentNameEdittext = (EditText)
// findViewById(R.id.studentNameEdittext);
startYourStoryButton = (Button) findViewById(R.id.startYourStoryButton);
// loginButton = (Button) findViewById(R.id.loginButton);
navCollegesButton = (Button) findViewById(R.id.navCollegesButton);
// settingsButton = (Button) findViewById(R.id.settingsButton);
// search_friends_button = (Button)
// findViewById(R.id.search_friends_button);
browseStoriesButton = (Button) findViewById(R.id.browseStoriesButton);
// textView1 = (TextView) findViewById(R.id.textView1);

// Set up font type
// studentNameEdittext.setTypeface(TypeFaceController.generalTextFace(HomeActivity.this));
startYourStoryButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// loginButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
navCollegesButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// settingsButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// search_friends_button.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
browseStoriesButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// textView1.setTypeface(TypeFaceController.titleFace(HomeActivity.this));

startYourStoryButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0){
Intent i = new Intent(HomeActivity.this, SignUp.class);
i.putExtra("signUpCaller", "Home");
startActivity(i);
}
});

// loginButton.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View arg0){
// Intent i = new Intent(HomeActivity.this, Login.class);
// startActivity(i);
// }
// });

// search_friends_button.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v){
// Intent i = new Intent(HomeActivity.this,
// FindStudentBrowseStoryActivity.class);
// i.putExtra("Button", "search_friends_button");
// i.putExtra("searchString", studentNameEdittext.getText().toString());
// startActivity(i);
//
// }
// });

browseStoriesButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v){
Intent i = new Intent(HomeActivity.this, FindStudentBrowseStoryActivity.class);
i.putExtra("Button", "browseStoriesButton");
startActivity(i);

}
});

navCollegesButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v){
Intent i = new Intent(HomeActivity.this, CollegeListActivity.class);
startActivity(i);

}
});

// settingsButton.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v){
// Intent i = new Intent(HomeActivity.this, HomeSettingsActivity.class);
// i.putExtra("FromActivity", "HomeSettingsActivity");
// startActivity(i);
// finish();
//
// }
// });

}

// @Override
// protected void onStart(){
// if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
// finish();
// }
// super.onStart();
// }
//
// @Override
// protected void onResume(){
// if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
// finish();
// }
// super.onResume();
// }

// =========Login button action bar
@Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_menu, menu);
return true;
}

// =========Login button action bar
@Override
public boolean onOptionsItemSelected(MenuItem item){
// handle item selection
switch (item.getItemId()) {
case R.id.home_login_string:
Intent i = new Intent(HomeActivity.this, Login.class);
// finish();
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

// logic to fix logout

@Override
public void onBackPressed(){
// Intent startMain = new Intent(Intent.ACTION_MAIN);
// startMain.addCategory(Intent.CATEGORY_HOME);
// startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);;
// startActivity(startMain);
//
// int pid = android.os.Process.myPid(); //
// android.os.Process.killProcess(pid); // return; }
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}

// method to save variable in preference
public void saveInPreference(String name, String content){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(name, content);
editor.commit();
}

// getting content from preferences
public String getFromPreference(String variable_name){
String preference_return;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preference_return = preferences.getString(variable_name, "");

return preference_return;
}
}

请告诉我哪里出错了?为什么 Activity 会堆叠?

P.S:我们不能使用单顶等,因为它会导致现有自定义主题出现一些过渡问题!

最佳答案

获取一个扩展 Application 的应用程序类并获取一个 arrayList 并在 arraylist 中维护 Activity 的引用。

当您在所需 Activity 中单击后退按钮时,然后使用 arraylist 完成所有 Activity 。

进行一项基础 Activity 。哪个是所有 Activity 的父类(super class)

public class BaseActviity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App application = (App) getApplication();
application.addActivity(this);
}
}

Take one application class

public class App extends Application {
public App() {
if (listActivty == null) {
listActivty = new ArrayList<BaseActviity>();
}
}

public ArrayList<BaseActviity> listActivty;

public void addActivity(BaseActviity actviity) {
if (!listActivty.contains(actviity)) {
listActivty.add(actviity);
}
}
}

take 4 samples activity classes like ......

1) public class FirstActivity extends BaseActviity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
}

public void send(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
2) public class SecondActivity extends BaseActviity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
}

public void send(View view) {
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
}
3).........................
4) ............................
in 4 th activity class placed the following code

In desire activity that means final activity,override the onBackPressed

public void onBackPressed() {

super.onBackPressed();
App application = (App) getApplication();
ArrayList<BaseActviity> listActivty = application.listActivty;
for (BaseActviity actviity : listActivty) {
actviity.finish();

}
}

关于java - 相同的 Activity 堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27164527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com