Android后退按钮带我回到相同的视图,但该类似乎有变化

时间:2014-07-28 13:57:30

标签: java android back-button

我已经开发了我的第一个应用程序,现在我遇到了一些问题 我尝试在线寻找解决方案,但事实并非如此。所以也许有人可以帮助我。

我在我的应用程序中有不同的活动...设置菜单中的onClick调用它,它打开正确,但如果我按下后退按钮,它仍然显示相同的活动视图(并且只显示上一个活动的菜单)。活动和工作是菜单的按钮,但不是主视图中显示的按钮。

public class MyActivity extends Activity { 
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
  /*  if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }*/
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    try {
        String destPath = "/data/data/" + getPackageName() + "/databases/WizardDB";
        File f = new File(destPath);
        if (!f.exists()) {
            CopyDB(getBaseContext().getAssets().open("WizardDB"),
                    new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    new LongOperation().execute("");
}

菜单打开的活动

       package com.example.frank.myapplication;    
        import android.app.Activity;
        import android.content.Intent;
        import android.database.Cursor;
        import android.os.Bundle;
        import android.view.KeyEvent;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.WindowManager;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.TextView;
        import android.widget.Toast;    
        import org.w3c.dom.Text;    
        import java.util.ArrayList;
        import java.util.List;

public class showgames extends Activity {
     private List<String> listSpiel_Id;
     DBAdapter db = new DBAdapter(this);

        @Override

        protected void onCreate(Bundle savedInstanceState) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.showgames);
            Intent EingabeName = getIntent();
           // Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            String[] favoriteTVShows={};
            List<String> list = new ArrayList<String>();
            listSpiel_Id = new ArrayList<String>();
            db.open();
            Cursor c=db.getAllRecords();
            if (c.moveToFirst())
            {
                do {
                    String Beendet="";
                    String Ort="";
                    if (c.getInt(9)==1) {Beendet=getString(R.string.beendet);} else     {Beendet=getString(R.string.offen);}

                    if (c.getString(10) != null && !c.getString(10).isEmpty()) {
                        Ort=c.getString(10)+" - ";
                    }
                    //android.text.format.DateFormat.format("dd.MM.yyyy- hh:mm", c.getLong(2));
                    String Spiel=android.text.format.DateFormat.format("dd.M.yy k:mm",c.getLong(2))+" - "+Ort+c.getString(3)+" "+c.getString(4)+" "+c.getString(5)+" "+c.getString(6)+" "+c.getString(7)+" "+c.getString(8)+Beendet;
                    list.add(Spiel);
                    listSpiel_Id.add(c.getString(0));

                } while (c.moveToNext());
            } else {

                TextView Zuruecksetzen1 = (TextView) findViewById(R.id.Spieleanzeigen);
                Zuruecksetzen1.setText(getString(R.string.Spielwaehlenleer));
                Button ButtonLoeschen = (Button) findViewById(R.id.ButtonDatenloeschen);
                ButtonLoeschen.setVisibility(View.INVISIBLE);
            }


            /*String[] favoriteTVShows = {"Pushing Daisies", "Better Off Ted",
                    "Twin Peaks", "Freaks and Geeks", "Orphan Black", "Walking Dead",
                    "Breaking Bad", "The 400", "Alphas", "Life on Mars","Pushing Daisies", "Better Off Ted",
                    "Twin Peaks", "Freaks and Geeks", "Orphan Black", "Walking Dead",
                    "Breaking Bad", "The 400", "Alphas", "Life on Mars"};*/

            // The ListAdapter acts as a bridge between the data and each ListItem
            // You fill the ListView with a ListAdapter. You pass it a context represented by
            // this. A Context provides access to resources you need.
            // android.R.layout.simple_list_item_1 is one of the resources needed.
            // It is a predefined layout provided by Android that stands in as a default

            ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    list);

            // ListViews display data in a scrollable list
            ListView theListView = (ListView) findViewById(R.id.theListView);

            // Tells the ListView what data to use
            theListView.setAdapter(theAdapter);

            theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    String tvShowPicked = "You selected " +
                            String.valueOf(adapterView.getItemAtPosition(i));

                  //  Toast.makeText(showgames.this, listSpiel_Id.get(i), Toast.LENGTH_SHORT).show();
                    Intent getNameScreenIntent = new Intent(showgames.this, table.class);
                    getNameScreenIntent.putExtra("Spiel_ID", Long.valueOf(listSpiel_Id.get(i)));
                    db.open();
                    int Spielerzahl=db.getZahlderSpieler(Long.valueOf(listSpiel_Id.get(i)));
                    db.close();
                    ((MyApplication) showgames.this.getApplication()).setSpielerzahl(Spielerzahl);
                    startActivity(getNameScreenIntent);
                }
            });
        //    Toast.makeText(this, "Spiele anzeigen", Toast.LENGTH_LONG).show();
        }

    public void OnClickDatenLoeschen(View view) {
    db.Tabellenleeren();
        Toast.makeText(this, "Alle bisherigen Spiele gelöscht", Toast.LENGTH_LONG).show();
        setContentView(R.layout.activity_my);
        Intent getNameScreenIntent = new Intent(this, MyActivity.class);

        startActivity(getNameScreenIntent);
    }
    }

清单文件:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.example.frank.myapplication">
<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:name="MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    >
    <activity android:name="com.example.frank.myapplication.AndroidDatabaseManager" />
    <activity android:name="com.example.frank.myapplication.getcurrentlocation" />
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".table"
        android:parentActivityName=".MyActivity"
        android:theme="@style/AppTheme" />
    <activity
        android:name="com.example.frank.myapplication.showgames"
        android:parentActivityName=".MyActivity"
        android:screenOrientation="portrait"

        android:theme="@style/AppTheme" />
</application>

难道它仍然认为menubutton被按下了吗?

    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
         if (id == R.id.action_settings) {
             Toast.makeText(this, "huhu", Toast.LENGTH_LONG).show();
             return true;     
         }
         if (id == R.id.action_showlist) {
             setContentView(R.layout.showgames);
             Intent a = new Intent(this, showgames.class);
             a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(a);
             //Intent getNameScreenIntent = new Intent(this, showgames.class);
             //startActivity(getNameScreenIntent);
             //Toast.makeText(this, "Spiele anzeigen", Toast.LENGTH_LONG).show();
             return true;

         }

         return super.onOptionsItemSelected(item);
     }

如果有人能告诉我为什么第一项活动不再出现,那会很可爱。

1 个答案:

答案 0 :(得分:0)


你在谈论按下按钮,但我没有看到任何方法的实现

 onKeyDown(int keyCode, KeyEvent event)

所以重写此方法,并且:

删除它:

 if (id == R.id.action_showlist) {
 -->>> setContentView(R.layout.showgames);

活动“showgames”将在onCreate方法上设置布局。