我一直在尝试解决这个问题一段时间,我确信有办法解决这个错误/问题,但我还没有找到。
我遇到的问题是,一旦我通过后退按钮或主页按钮退出应用程序,它就会关闭,然后重新打开我之前打开的一项活动。
一旦我按下其中一个按钮,我是否需要关闭我的活动,我将如何去做。
这是我的主要活动的代码,它打开了我的其他活动......
package com.togdem.tunnels;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class TunnelsActivity extends Activity
{
int vo = 2;
String mla = "";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView p_btn = (ImageView) findViewById(R.id.imageView1);
p_btn.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
Intent myIntent2 = new Intent(view.getContext(), Cgt.class);
startActivity(myIntent2);
return false;
}
});
SeekBar skb = (SeekBar) findViewById(R.id.seekBar1);
try {
InputStream in = openFileInput("sa.txt");
if (in != null) {
InputStreamReader input = new InputStreamReader(in);
BufferedReader buffreader = new BufferedReader(input);
mla = "";
String line = new String();
line = buffreader.readLine();
mla += line;
Toast msg = Toast.makeText(getBaseContext(), "Loaded Saved Data", Toast.LENGTH_LONG);
msg.show();
in.close();
skb.setProgress(Integer.parseInt(mla.toString()));
}
else{}
} catch(Exception e){}
skb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar event, int value, boolean sb)
{
vo = value;
}
public void onStartTrackingTouch(SeekBar event)
{
}
public void onStopTrackingTouch(SeekBar event)
{
try
{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("sa.txt", 0));
out.write(Integer.toString(vo));
out.close();
} catch (IOException e)
{
}
Toast msg = Toast.makeText(getBaseContext(), "Set Sensitivity to: " + Integer.toString(vo), Toast.LENGTH_LONG);
msg.show();
}
});
}
}
这是我要打开的活动的代码...
package com.togdem.tunnels;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class Cgt extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.cgame );
ImageView btn1 = (ImageView) findViewById(R.id.btn_c);
ImageView btn2 = (ImageView) findViewById(R.id.btn_h);
btn1.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
try
{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("spd.txt", 0));
out.write("4");
out.close();
} catch (java.io.IOException e) {}
Intent myIntent = new Intent(view.getContext(), TunnelsSpA.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myIntent);
finish();
return false;
}
});
btn2.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
try
{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("spd.txt", 0));
out.write("6");
out.close();
} catch (java.io.IOException e) {}
Intent myIntent = new Intent(view.getContext(), TunnelsSpA.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myIntent);
finish();
return false;
}
});
}
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if (KeyCode == KeyEvent.KEYCODE_BACK);
{
onStop();
}
if (KeyCode == KeyEvent.KEYCODE_HOME);
{
onStop();
}
return super.onKeyDown(KeyCode, event);
}
protected void onStop()
{
finish();
Log.d("Stopping...", "Stopping the activity");
}
}
感谢你给与我的帮助,