I finally managed to implement StartApp into my cordova project and when everything looked nice the biggest problem appeared. StartApp somehow messes up my localstorage database. This also happends with adMob as I saw on the internet. When I hold the middle button to get into the recent apps and I force close my app, next time I open it everything from localstorage seems to be whiped out. I'm not sure how but if I enter and close it a few more times the localstorage gets his old values, than again looses them.
I'm sure its Startapp because if I comment:
StartAppSDK.init(this, "xxx", "xxx", true);
everything works fine again.
I read on the internet that it may be a conflict between StartApp (and also happens to adMob) and the localstorage.
One way is to delay the StartApp initialisation like I've done in my code. It seems this is working but I get another issue:
after the banner is loaded up through this method: it refreshed each and every second instead of 5-6 seconds as before and this is really frustrating (the banner almost rolls unlimited and I cant really read the ad). And another issue to this workaround is that the banner misses a few pixels at left and right of it. So the width is a little shrinked in and doesn't look nice.
Notice: I noticed that indifferent what method I use to initialize StartApp when the device is onPause I get FAILED BINDER TRANSACTION in logcat. But everything works fine. The code in onPuse and onResume gets triggered. I'm guessing maybe this is the cause of StartApp messing up the localstorage(because the thing only happends when I force close the app, and before on force close onPause event gets triggered).
And sometimes at onResume or on start of the app I get: E/Web Console: Viewport argument key "" not recognized and ignored. at http://www.startappexchange.com/:20
Also without any effect on my ap but maybe it may have effect on the localstorage..
Main java file:
package com.Snap.What;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.widget.RelativeLayout;
import org.apache.cordova.*;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.banner.Banner;
public class WhatSnap extends CordovaActivity
{
private StartAppAd startAppAd = new StartAppAd(this);
private ViewGroup layout;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
appView.getSettings().setJavaScriptEnabled(true);
appView.addJavascriptInterface(jsInterface, "JSInterface");
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html");
mHandler.postDelayed(new Runnable() {
public void run() {
doStuff();
}
}, 5000);
setLayout();
// showBanner();
}
public void doStuff(){
StartAppSDK.init(this, "xxx", "xxx", true);
startAppAd.loadAd(); // load the next ad
}
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
@JavascriptInterface
public void displayBanner()
{
activity.runOnUiThread(new Runnable() {
public void run() {
//Code that interact with UI
initBanner();
}
});
}
@JavascriptInterface
public void displayexitAd()
{
activity.runOnUiThread(new Runnable() {
public void run() {
//Code that interact with UI
exitAd();
}
});
}
@JavascriptInterface
public void displayAd()
{
activity.runOnUiThread(new Runnable() {
public void run() {
//Code that interact with UI
showAd();
}
});
}
}
public void setLayout(){
root.removeView((View) appView);
layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(lp);
appView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
appView.setVisibility(View.INVISIBLE);
layout.addView((View) appView);
setContentView(layout);
Log.e("setLayout", "setLayout");
}
public void initBanner(){
Banner startAppBanner = new Banner(this);
RelativeLayout.LayoutParams bannerParameters =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
// Add to main Layout
layout.addView(startAppBanner, bannerParameters);
Log.e("showBanner", "showBanner");
}
public void exitAd(){
startAppAd.onBackPressed();
}
public void showAd(){
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); // load the next ad
}
}
AndroidManifest.xml:
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.Snap.What" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:launchMode="singleTop" android:name="WhatSnap" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.startapp.android.publish.list3d.List3DActivity"
android:theme="@android:style/Theme" />
<activity android:name="com.startapp.android.publish.AppWallActivity"
android:theme="@android:style/Theme.Translucent"
android:configChanges="orientation|keyboardHidden|screenSize" />
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
I also saw anothe possible workaround to drop the mHandler code and to override onAttachedToWindow like:
@Override public void onAttachedToWindow() {
super.onAttachedToWindow();
doStuff();
};
But sometimes when I force close the app the localstorage remains sometimes not.
Please help me, I stayed 3 days to solve this issue and nothing good ever happened.