2

我正在使用Titanium 3.1並針對Android 3.0及更高版本進行開發。如何在Titanium開發的Android應用程序中保存實例狀態?

我的應用程序有一個視圖,當點擊時詢問您是否要拍照或從圖庫中選擇圖像。當我選擇從相機拍攝照片時,相機顯示沒有問題,我可以拍攝照片,問題是在拍攝照片並選擇使用照片後,我的應用程序從頭開始恢復,而不是返回到之前選擇拍照前的狀態。

當我檢查logcat的我看到這行:

I/TiRootActivity(24120): (main) [0,0] checkpoint, on root activity create, savedInstanceState: null 

看來我的應用程序的狀態不會被保存,但我不知道爲什麼。我會誠實地說,這是我第一次開發一款應用程序,該應用程序會轉到相機應用程序,拍攝照片並返回到應用程序。以前我曾在Titanium中使用Intents,並且在退出使用後退按鈕用Intent打開的應用程序後,我已能夠返回到我的應用程序的正確狀態。

這是我用它來打開攝像頭的代碼:

var globalBabyPicture = Titanium.UI.createImageView({ 
    image:imagesPath + "kinedu_0027_ic_camara.png", 
    width:75, 
}); 

var photoOptionsViewFromCamera = Ti.UI.createView({ 
    width:Ti.Platform.displayCaps.platformWidth, 
    height:44, 
    left:0, 
    top:1*44, 
    backgroundColor:"transparent" 
}); 

var photoOptionsViewFromCameraLabel = Ti.UI.createLabel({ 
    text:"from camera", 
    font:{fontSize:14, fontFamily:"Arial Rounded MT Bold"}, 
    color:"#368cd6" 
}); 

photoOptionsViewFromCamera.add(photoOptionsViewFromCameraLabel); 

photoOptionsViewFromCamera.addEventListener("touchstart", function(e){ 
    var animateTouchStart = Ti.UI.createAnimation({backgroundColor:"#AFD1DE", duration:150}); 
    photoOptionsViewFromCamera.animate(animateTouchStart); 

}); 

//********* this is the code that triggers the camera to take the picture 
photoOptionsViewFromCamera.addEventListener("touchend", function(e){ 
    var animateTouchEnd = Ti.UI.createAnimation({backgroundColor:"transparent", duration:150}); 
    photoOptionsViewFromCamera.animate(animateTouchEnd); 

    animateTouchEnd.addEventListener("complete", function(e){ 
     Ti.Media.showCamera({ 
      success : function(event) { 

       var tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.png')); 
        tmp.write(event.media); 
        var blob = tmp.read(); 

        Ti.App.fireEvent("changePicture"); 

       }, 
      cancel : function() { 
      }, 
      error : function(error) { 
       var message; 
       if (error.code == Ti.Media.NO_CAMERA) { 
        message = 'Device does not have camera capabilities'; 
       } else { 
        message = 'Unexpected error: ' + error.code; 
       } 

       Ti.UI.createAlertDialog({ 
        title : 'Camera', 
        message : message 
       }).show(); 
      }, 
      saveToPhotoGallery : false, 
      allowEditing : true, 
      mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] 
     });  
    }); 
}); 


Ti.App.addEventListener("changePicture", function(e){ 
    var tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.png')); 
    var blob = tmp.read(); 

    var animationChange = Ti.UI.createAnimation({opacity:0, duration:200}); 

    babyImage.animate(animationChange); 

    var animationChangeCompleted = Ti.UI.createAnimation({opacity:1, duration:200}); 

    animationChange.addEventListener("complete", function(e){ 
     babyImage.setWidth(100); 
     var image = blob.imageAsThumbnail(150);       
     babyImage.setImage(image);    
     babyImage.animate(animationChangeCompleted); 
    });    
}); 

我檢查,併成功回調從來沒有,我認爲這是因爲應用程序從一開始恢復,顯示應用程序啓動畫面。

如何確保在拍攝照片後,應用程序返回到前一個視圖,而不必從頭開始恢復應用程序?

+0

如果您的應用展示飛濺可能崩潰,而你與恢復從一開始就應用混淆了。 我建議從adb中檢查logcat以及Titanium控制檯,並在這裏粘貼日誌。 – Dragon

回答

相關問題