Archive

Posts Tagged ‘activity’

Create application with multiple entry-point activities

June 4, 2012 2 comments

A great stuff I’ve got today is that “An application can have many different entry-point activities.”

Here the sample, I create a project with four Activities: First Act, Second Act, Third Act, Four Act

Multiple entry-point Activites for one application

Multiple entry-point Activites for one application

The trick behind this stuff is to register your desired activities on AndroidManifest.xml w/ <intent-filter> is MAIN.

The sample for my sample case it should be like this:

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="First Act"
            android:name=".FirstAct"
            android:icon="@drawable/ic_first_act"
            android:taskAffinity=".FirstAct"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:label="Second Act"
            android:name=".SecondAct"
            android:icon="@drawable/ic_second_act"
            android:taskAffinity=".SecondAct"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:label="Third Act"
            android:name=".ThirdAct"
            android:icon="@drawable/ic_third_act"
            android:taskAffinity=".ThirdAct"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <activity
            android:label="Four Act"
            android:name=".FourAct"
            android:icon="@drawable/ic_four_act"
            android:taskAffinity=".FourAct"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

There are some applications done this before. As I remember the app: “Facebook for Android” in version 1.9.0 (release on 19/Apr/2012) containing three activites registering to MAIN/LAUNCHER.

Cheers,

Pete Houston

Categories: Home Tags: , ,

The less-known over screen orientation

September 19, 2011 Leave a comment

I’ve just found a really interesting problem today at work, it’s about screen orientation.

I was trying to create a very simple application, which:

+ contains one button to change screen orientation state: PORTRAIT to LANDSCAPE, and vice versa.

+ the text on button determines the action going to be done if click. If the current is PORTRAIT then the text should be LANDSCAPE, and vice versa.

OK, so here is one very simple implementation, easy-read, easy-understood:

package pete.android.study;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    // the constant strings defining text to display on button
	static final String ORIENT_PORTRAIT = "Set Portrait";
	static final String ORIENT_LANDSCAPE = "Set Landscape";
	// determine screen orientation state changed
	boolean mState = false;
	// the main button
	Button mBtnSet = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // load button from layout
        mBtnSet = (Button)findViewById(R.id.btnSet);
        // set default text display
        mBtnSet.setText(ORIENT_LANDSCAPE);
        // handle click event
        mBtnSet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// state FALSE: switch to LANDSCAPE
				if(!mState) {
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
					mBtnSet.setText(ORIENT_PORTRAIT);
				}
				// state TRUE: switch to PORTRAIT
				else {
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
					mBtnSet.setText(ORIENT_LANDSCAPE);
				}
				// update state
				mState = !mState;
			}
		});
    }
}

However, there has a problem inside. First, try to build and run it.

The Initial Screen

The Initial Screen

By default, the screen is set as PORTRAIT, and the text is “Set Landscape“, which is correct.

Next, try to click on button, this will happen:

After First Click on Button

After First Click on Button

The screen orientation is changed to LANDSCAPE, however, the text on button doesn’t change at all. That’s so surprising!

And keep clicking on button one more time:

The 2nd Click

The 2nd Click

Now, the text changes to “Set Portrait“, well, the orientation doesn’t change, but on the third click, the screen interface backs to the first initial state.

It’s interesting, isn’t it?

The question in mind is why it is…

First, reading this one on Google Android References – Activity#ConfigurationChanges

Reading clearly, you’ll find that, everytime the screen orientation changes, the Activity is auto destroyed and recreated again, which means the text “Set Landscape” is reset as the initial state, a re-call of onCreate() method.

That’s the problem!

The solution is very simple, by adding the attribute – android:configChanges=”orientation” – , it will avoid re-create the activity and keep the current state.

Note: if you always to handle screen orientation without attribute of handling orientation, sometimes you might get exception, or errors in your application as well, even crash-out and freeze. So always keep this in mind :).

Hope you having fun with this little tips!

 

Cheers,

Pete Houston

 

 

 

ListView in one activity doesn’t refresh data sending another activity

July 15, 2011 1 comment

There’s a problem in my previous post on Auto Receiving SMS that the ListView in MainActivity doesn’t refresh the underlying data which is sent from BroadcastReceiver.

The reason is:

– When application is load, the onCreate() of MainActivity is called, at this time there’s no point of new arriving message, and that’s done! Data will never update more to the ListView.

The solution:
– While SmsReceiver continuously waits for new message and sends it directly to MainActivity, the new message will be stored on the Intent of the automatically-triggered onNewIntent() method.

– What we need to do is to handle new message inside this method.

– This is the update version of MainActivity

package pete.android.study;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView mListData;
    ArrayList<SmsInfo> mListSms = new ArrayList<SmsInfo>();
    SmsInfoAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mListData = (ListView)findViewById(R.id.lvData);
    }

    @Override
    protected void onNewIntent(Intent data) {
    	ArrayList<SmsInfo> tmp = data.getParcelableArrayListExtra("ListSMS");
    	mListSms.add(tmp.get(tmp.size() - 1));

    	if(mAdapter == null) {
    		mAdapter = new SmsInfoAdapter(this, mListSms);
    		mListData.setAdapter(mAdapter);
    		return;
    	}
    	mAdapter.notifyDataSetChanged();
    }
}

– Yes, at first time when new message arrives, the adapter is null and it should be created.

– The next time it’s not null but we need to notify the ListView about data changed.

Question here: why do I receive new message storing in a temp ArrayList instead of directly using mListSms itself?

The answer is: I don’t want to change the reference of data in which I already set into the adapter at the first time. Assigning mListSms to the new message ArrayList means to create new ArrayList<SmsInfo> not using the old one. If I do, the, calling of mAdapter.notifyDataSetChanged() won’t respond or do nothing because it doesn’t have reference to the old list. Also, the new message arriving always stays at last position in the sending list from SmsReceiver, so I just need to add the last item from temp list is enough.

 

Hope you learn something from this!

 

Cheers,

Pete Houston

Launch an activity

June 12, 2011 11 comments

This is how you can launch an activity:

	public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
		// query the intent for lauching
		Intent intent = pm.getLaunchIntentForPackage(pkgName);
		// if intent is available
		if(intent != null) {
			try {
				// launch application
				c.startActivity(intent);
				// if succeed
				return true;

			// if fail
			} catch(ActivityNotFoundException ex) {
				// quick message notification
				Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
				// display message
				toast.show();
			}
		}
		// by default, fail to launch
		return false;
	}

Just customize and make use of it.
Hope you enjoy!

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , , ,

Activity with no title bar


If you don’t want your Activity to have title bar, then just disable it.

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // <--- this is what u need
        setContentView(R.layout.main);
    }
}

This is what u have after.

No Title Bar

No Title Bar

Hope you enjoy it!

 

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , , , ,