Categories
discuss

How can I save an activity state using the save instance state?

I’ve been working on the Android SDK platform, and it is a little unclear how to save an application’s state. So given this minor re-tooling of the ‘Hello, Android’ example:

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {

  private TextView mTextView = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTextView = new TextView(this);

    if (savedInstanceState == null) {
       mTextView.setText("Welcome to HelloAndroid!");
    } else {
       mTextView.setText("Welcome back.");
    }

    setContentView(mTextView);
  }
}

I thought it would be enough for the simplest case, but it always responds with the first message, no matter how I navigate away from the app.

I’m sure the solution is as simple as overriding onPause or something like that, but I’ve been poking away in the documentation for 30 minutes or so and haven’t found anything obvious.

Answer

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
  savedInstanceState.putString("MyString", "Welcome back to Android");
  // etc.
}

The Bundle is essentially a way of storing a NVP (“Name-Value Pair”) map, and it will get passed in to onCreate() and also onRestoreInstanceState() where you would then extract the values from activity like this:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
}

Or from a fragment.

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
    double myDouble = savedInstanceState.getDouble("myDouble");
    int myInt = savedInstanceState.getInt("MyInt");
    String myString = savedInstanceState.getString("MyString");
}

You would usually use this technique to store instance values for your application (selections, unsaved text, etc.).

Categories
discuss

How do I execute a page-defined JavaScript function from a Firefox extension?

I’m creating a Firefox extension for demo purposes. I to call a specific JavaScript function in the document from the extension. I wrote this in my HTML document (not inside extension, but a page that is loaded by Firefox):

document.funcToBeCalled = function() {
   // function body
};

Then, the extension will run this on some event:

var document = Application.activeWindow.activeTab.document;
document.funcToBeCalled();

However it raises an error saying that funcToBeCalled is not defined.

Note: I could get an element on the document by calling document.getElementById(id);

Answer

It is for security reasons that you have limited access to the content page from extension. See XPCNativeWrapper and Safely accessing content DOM from chrome,

If you control the page, the best way to do this is set up an event listener in the page and dispatch an event from your extension (addEventListener in the page, dispatchEvent in the extension).

Otherwise, see http://groups.google.com/group/mozilla.dev.extensions/msg/bdf1de5fb305d365

Categories
discuss

Explain the JVM Directory Layout on Mac OSX Leopard

Here is the directory layout that was installed with Leopard. What is the “A” directory and why the “Current” directory in addition to the “CurrentJDK”?

It seems like you can easily switch the current JDK by move the CurrentJDK link, but then the contents under Current and A will be out of sync.

lrwxr-xr-x  1 root  wheel    5 Jun 14 15:49 1.3 -> 1.3.1
drwxr-xr-x  3 root  wheel  102 Jan 14  2008 1.3.1
lrwxr-xr-x  1 root  wheel    5 Feb 21  2008 1.4 -> 1.4.2
lrwxr-xr-x  1 root  wheel    3 Jun 14 15:49 1.4.1 -> 1.4
drwxr-xr-x  8 root  wheel  272 Feb 21  2008 1.4.2
lrwxr-xr-x  1 root  wheel    5 Feb 21  2008 1.5 -> 1.5.0
drwxr-xr-x  8 root  wheel  272 Feb 21  2008 1.5.0
lrwxr-xr-x  1 root  wheel    5 Jun 14 15:49 1.6 -> 1.6.0
drwxr-xr-x  8 root  wheel  272 Jun 14 15:49 1.6.0
drwxr-xr-x  8 root  wheel  272 Jun 14 15:49 A
lrwxr-xr-x  1 root  wheel    1 Jun 14 15:49 Current -> A
lrwxr-xr-x  1 root  wheel    3 Jun 14 15:49 CurrentJDK -> 1.5
steve-mbp /System/Library/Frameworks/JavaVM.framework/Versions $ 

and the contents of A

-rw-r--r--   1 root  wheel    1925 Feb 29  2008 CodeResources
drwxr-xr-x  34 root  wheel    1156 Jun 14 15:49 Commands
drwxr-xr-x   3 root  wheel     102 Mar  6  2008 Frameworks
drwxr-xr-x  16 root  wheel     544 Jun 14 15:49 Headers
-rwxr-xr-x   1 root  wheel  236080 Feb 29  2008 JavaVM
drwxr-xr-x  29 root  wheel     986 Jun 14 15:49 Resources
steve-mbp /System/Library/Frameworks/JavaVM.framework/Versions/A $ 

Answer

The (A, Current symbolic-linked to A) is part of the structure of a Mac OS X framework, which JavaVM.framework is. This framework may have C or Objective-C code in it, in addition to the actual JVM installations. Thus it could potentially be linked against from some C or Objective-C code in addition to containing the JVM alongside that.

Note that you should not change the CurrentJDK link to point at anything but what it is set to by Mac OS X. Unlike on other platforms, the Java virtual machine is an operating system service on Mac OS X, and changing it in this way would put you in an unsupported (and potentially untested, unstable, etc.) configuration.

Categories
discuss

Regular expression to match non-ASCII characters?

What is the easiest way to match non-ASCII characters in a regex? I would like to match all words individually in an input string, but the language may not be English, so I will need to match things like ü, ö, ß, and ñ. Also, this is in Javascript/jQuery, so any solution will need to apply to that.

Answer

This should do it:

[^x00-x7F]+

It matches any character which is not contained in the ASCII character set (0-127, i.e. 0x0 to 0x7F).

You can do the same thing with Unicode:

[^u0000-u007F]+

For unicode you can look at this 2 resources:

Categories
discuss

Creating non-reverse-engineerable Java programs

Is there a way to deploy a Java program in a format that is not reverse-engineerable?

I know how to convert my application into an executable JAR file, but I want to make sure that the code cannot be reverse engineered, or at least, not easily.

Obfuscation of the source code doesn’t count… it makes it harder to understand the code, but does not hide it.

A related question is How to lock compiled Java classes to prevent decompilation?


Once I’ve completed the program, I would still have access to the original source, so maintaining the application would not be the problem. If the application is distributed, I would not want any of the users to be able to decompile it. Obfuscation does not achieve this as the users would still be able to decompile it, and while they would have difficulty following the action flows, they would be able to see the code, and potentially take information out of it.

What I’m concerned about is if there is any information in the code relating to remote access. There is a host to which the application connects using a user-id and password provided by the user. Is there a way to hide the host’s address from the user, if that address is located inside the source code?

Answer

You could obfuscate your JAR file with YGuard. It doesn’t obfuscate your source code, but the compiled classes, so there is no problem about maintaining the code later.

If you want to hide some string, you could encrypt it, making it harder to get it through looking at the source code (it is even better if you obfuscate the JAR file).

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..