我刚接触到android。我创建简单的应用程序。在我的主页Screeen中有Viewpager&带有2个片段的标签。在每个片段中都有一个listview。
问题是当按下后退按钮2次然后显示白色屏幕时,在白色屏幕上再按1次后退按钮退出。
我不知道我的错误在哪里。
活动代码如下。
package com.example.view;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.astuetz.PagerSlidingTabStrip;
import com.example.adapter.ViewPagerAdapter;
import com.example.cardholder.BaseActivity;
import com.example.cardholder.R;
import com.example.service.DownloadService;
import com.example.service.UploadService;
public class HomeActivity extends BaseActivity implements OnPageChangeListener {
Toolbar toolbar;
// TextView _TextViewToolbar;
ViewPager pager;
ViewPagerAdapter adapter;
CharSequence Titles[] = { "Card Holder", "Contacts" };
int Numboftabs = 2;
private Boolean exit = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
// _TextViewToolbar = (TextView)
// toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles,
Numboftabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setOnPageChangeListener(this);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
// tabs.setShouldExpand(true);
tabs.setDividerPadding(2);
tabs.setDividerColor(getResources().getColor(R.color.colorPrimary));
tabs.setTextColor(Color.WHITE);
tabs.setIndicatorHeight(4);
tabs.setIndicatorColor(Color.WHITE);
// tabs.setIndicatorColor(getResources().getColor(R.color.colorPrimaryDark));
}
private void logOut() {
logOutSharedPreference();
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
@Override
public void onBackPressed() {
if (exit) {
super.onBackPressed();
} else {
showToast(HomeActivity.this, "Press Back again to Exit.");
this.exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 2000);
}
/*AlertDialog.Builder builder = new AlertDialog.Builder(this,
R.style.AppCompatAlertDialogStyleMsgAlert);
builder.setMessage("Are You Sure Want To Close ? ");
builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
HomeActivity.this.finish();
}
});
builder.show();*/
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.account_setting:
Intent _IntentAccountSetting = new Intent(HomeActivity.this,
AccountSettingActivity.class);
startActivity(_IntentAccountSetting);
break;
case R.id.logout:
logOut();
HomeActivity.this.finish();
break;
case R.id.upload:
if (!isInternetConnect()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this,
R.style.AppCompatAlertDialogStyleMsgAlert);
builder.setMessage("Required Internet Connection.");
builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// continue with delete
}
});
builder.show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this,
R.style.AppCompatAlertDialogStyleMsgAlert);
builder.setMessage("Are You Sure Want To Upload Data From "
+ getEmail(HomeActivity.this) + " ?");
builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent i1 = new Intent(HomeActivity.this,
UploadService.class);
startService(i1);
}
});
builder.setNegativeButton("CANCLE",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
builder.show();
}
break;
case R.id.download:
if (!isInternetConnect()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this,
R.style.AppCompatAlertDialogStyleMsgAlert);
builder.setMessage("Required Internet Connection.");
builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
builder.show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this,
R.style.AppCompatAlertDialogStyleMsgAlert);
builder.setMessage("Are You Sure Want To Download Data From "
+ getEmail(HomeActivity.this) + " ?");
builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent i1 = new Intent(HomeActivity.this,
DownloadService.class);
startService(i1);
}
});
builder.setNegativeButton("CANCLE",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
builder.show();
}
break;
case R.id.delete_cardholder:
Intent _IntentDeleteActivity = new Intent(HomeActivity.this,CardHoderDeleteActivity.class);
startActivity(_IntentDeleteActivity);
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
setTitle(adapter.getPageTitle(position));
}
}
答案 0 :(得分:0)
您的代码中存在以下问题:
@Override
public void onBackPressed() {
if (exit) {
super.onBackPressed();
} else {
showToast(HomeActivity.this, "Press Back again to Exit.");
this.exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 2000);
}
当你第一次按回时,退出是假的,所以不会调用super.onBackPressed。
只需将此代码替换为:
@Override
public void onBackPressed() {
super.onBackPressed();
//finish();
}
或者如果您没有在背面按下任何事件而只想完成活动,则删除该方法。