Difference standard, singleTop, singleInstance, singleTask

standard (default) :- Multiple instances of the activity class can be instantiated and multiple instances can be added to the same task or different tasks. This is the common mode for most of the activities.

singleTop :- The difference from standard is, if an instance of the activity already exists at the top of the current task and the system routes the intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object.

singleTask:- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.

singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance’s. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.

source: http://stackoverflow.com/questions/25773928/setting-launchmode-singletask-vs-setting-activity-launchmode-singletop

To translate for my language:

We have 3 activities A>B>C and we are now in C

  1. standard  – creates everytime new activity. There is nothing to explain.
  2. singleTop – when we are in C and we want startActivity A it will create another A, but when we startActivity C it won’t create it because C is on top
  3. singleTask – when we are in C and we want startActivity A, it will bring to front A, but when we startActivity C it won’t create it because C is the current task
  4. singleInstance – imho don’t know 😀 above explanation might help a bit

IntentService : has no zero argument constructor

Caused by: java.lang.InstantiationException: java.lang.Class<com.example.MyWakefulService> has no zero argument constructor

What does it means? We created custom IntentService and what? Do we have empty constructor? Nope, so let’s do it!

public class MyWakefulService extends IntentService {

    private static final String TAG = "MyWakefulService";

    public MyWakefulService() {
        super(TAG);
    }
}

Should be OK now.

Reading plain text file in java and android asset

//read lines from file

ArrayList<String> lines = new ArrayList<String>();
try {
    BufferedReader input = new BufferedReader(new FileReader(f));

    String line;
    do {
        if ((line = input.readLine()) != null)
            lines.add(line);
    } while (line != null);
} catch (IOException e) {
    e.printStackTrace();
}
return lines;

//read lines from file asset

StringBuilder sb = new StringBuilder();
try {
    BufferedReader input = new BufferedReader(
            new InputStreamReader(assets.open(fileName)));

    String line;
    do {
        if ((line = input.readLine()) != null)
            sb.append(line);
    } while (line != null);
} catch (IOException e) {
    e.printStackTrace();
}
return sb.toString();

Recyclerview with padding and clipping first item

As usual in example:

<android.support.v7.widget.RecyclerView
    android:clipToPadding="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

The golden trinity is:

android:clipToPadding="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"

More info here: http://stackoverflow.com/q/24914191/619673

Permanently block apk updating

All you need to do is: decompile apk, change code version of apk, compile again and sign with already not original key.

  1. apktool d app.apk
  2. open folder, find yml file, open it and change versionCode to i.e
  3. apktool b app folder
  4. output will be in app folder / dist folder
  5. use this command to sign app, instead you will not be able to install it
    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore my_application.apk alias_name

    i.e.

    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore "C:\Users\Me\.android\debug.keystore" app.apk androiddebugkey

     

  6. Done.

Information:

  1. jarsigner is placed in java pathfolder
  2. debug.keystore should be created first time when running own project in android studio

Attachment: apk tool