我有一個webview,我加載一個外部URL,所以這加載第一頁,我點擊鏈接去第二頁,然後我點擊第三頁。保存WebView加載其他活動?
現在,在這第三頁上我寫了一個Javascript來打開另一個活動工作正常。 但是在這個活動結束時,我的WebView重新加載了第一個頁面,而這個頁面應該是相同的頁面,在這個頁面中的Activity被調用。
任何想法到底是什麼,我做錯了?
在此先感謝。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.R.bool;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class Cam2 extends Activity {
int CAMERA_PIC_REQUEST = 2;
WebView webview;
String imageSource;
boolean _hasImage = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description,
Toast.LENGTH_SHORT).show();
}
@Override
public void onPageFinished(WebView view, String url) {
if (_hasImage) {
webview.loadUrl("javascript:AndroidSubmitToWeb();");
}
}
});
webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");
webview.loadUrl("http://192.168.0.5/SomeWeb");
}
public void openCamera() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
String s = storeAndExit(thumbnail);
webview.loadUrl("javascript:function ImageData(){ return '" + s
+ "';}");
_hasImage = true;
} else {
Toast.makeText(Cam2.this, "Picture Not taken", 5000).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
public String storeAndExit(Bitmap data) {
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
Bitmap myMap = data;
try {
if (myMap.compress(CompressFormat.JPEG, 70, jpeg_data)) {
byte[] code = jpeg_data.toByteArray();
byte[] output = org.apache.commons.codec.binary.Base64
.encodeBase64(code);
String js_out = new String(output);
return js_out;
}
} catch (Exception e) {
Toast.makeText(Cam2.this,
"STORE IMAGE EXCEPTION: " + e.getMessage(), 5000).show();
}
finish();
return null;
}
}
請檢查是否的onCreate()再次被調用,而你從你的第二個活動退出。 – 2011-05-18 08:19:59
是的,它調用onCreate() – user581157 2011-05-18 08:30:23