我已经在 Internet 上阅读了有关此线程的信息,但我还没有找到解决方案。Manifest 文件中的 :noHistory="true" 等解决方案。我想要的是,在用户单击 AddNewItem 类中的保存按钮后,它将用户重定向到 MainActivity 类,当用户单击后退按钮时,它将返回到 mainMenu,每一步都没有返回制成。
这是我的清单文件
<activity
android:name="com.example.shoppingapp.AddNewItems"
android:label=""
android:noHistory="true">
</activity>
这是我的主要活动>
public class MainActivity extends ListActivity {
// The Intent is used to issue that an operation should
// be performed
Intent intent;
TextView itemId;
// The object that allows me to manipulate the database
ShoppingDB shopdatabase = new ShoppingDB(this);
// Called when the Activity is first called
protected void onCreate(Bundle savedInstanceState) {
// Get saved data if there is any
super.onCreate(savedInstanceState);
// Designate that edit_contact.xml is the interface used
// is activity_main.xml
setContentView(R.layout.activity_main);
// Gets all the data from the database and stores it
// in an ArrayList
ArrayList<HashMap<String, String>> contactList = shopdatabase.getAllItems();
// Check to make sure there are contacts to display
if(contactList.size()!=0) {
// Get the ListView and assign an event handler to it
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When an item is clicked get the TextView
// with a matching checkId
itemId = (TextView) view.findViewById(R.id.itemId);
// Convert that itemId into a String
String itemIdValue = itemId.getText().toString();
// Signals an intention to do something
// getApplication() returns the application that owns
// this activity
Intent theIndent = new Intent(getApplication(),EditItemList.class);
// Put additional data in for EditContact to use
theIndent.putExtra("itemId", itemIdValue);
// Calls for EditContact
startActivity(theIndent);
}
});
// A list adapter is used bridge between a ListView and
// the ListViews data
// The SimpleAdapter connects the data in an ArrayList
// to the XML file
// First we pass in a Context to provide information needed
// about the application
// The ArrayList of data is next followed by the xml resource
// Then we have the names of the data in String format and
// their specific resource ids
ListAdapter adapter = new SimpleAdapter( MainActivity.this,contactList, R.layout.item_list_entry, new String[] { "itemId","textAmount", "textItem"}, new int[] {R.id.itemId, R.id.textAmount, R.id.textItem});
// setListAdapter provides the Cursor for the ListView
// The Cursor provides access to the database data
setListAdapter(adapter);
}
}
public void callStartApp(View view){
Intent startIntent = new Intent(getApplication(), StartingApp.class);
startActivity(startIntent);
}
// When showAddContact is called with a click the Activity
// NewContact is called
public void showAddItem(View view) {
Intent theIntent = new Intent(getApplicationContext(), AddNewItems.class);
startActivity(theIntent);
}
}
这是我的 AddNewItem 类
public class AddNewItems extends Activity {
// The EditText objects
EditText textItem;
EditText textAmount;
EditText textPrice;
EditText textPlace;
ShoppingDB shopdatabase = new ShoppingDB(this);
@Override
public void onCreate(Bundle savedInstanceState) {
// Get saved data if there is any
super.onCreate(savedInstanceState);
// Designate that add_new_item.xml is the interface used
setContentView(R.layout.add_item);
// Initialize the EditText objects
textItem = (EditText) findViewById(R.id.textItem);
textAmount = (EditText) findViewById(R.id.textAmount);
textPrice = (EditText) findViewById(R.id.textPrice);
textPlace = (EditText) findViewById(R.id.textPlace);
}
public void addNewItems(View view) {
// Will hold the HashMap of values
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
// Get the values from the EditText boxes
queryValuesMap.put("textItem", textItem.getText().toString());
queryValuesMap.put("textAmount", textAmount.getText().toString());
queryValuesMap.put("textPrice", textPrice.getText().toString());
queryValuesMap.put("textPlace", textPlace.getText().toString());
// Call for the HashMap to be added to the database
shopdatabase.insertItem(queryValuesMap);
// Call for MainActivity to execute
this.callMainActivity(view);
}
public void callMainActivity(View view) {
Intent theIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(theIntent);
}
}