Friday, February 28, 2014

How to set an activity dynamically for up-button in android

This is done by implementing getSupportParentActivityIntent() method  and using it we can dynamically set the activity to up-button in android. Here how I have achieved it.

Assume we have two activities. Activity A and B. A is the parent activity and B is the child.

So A need to create an intent to start B. It should pass an extra data which is the name of the parent activity. Here in our example it should be 'A'. Here is the code,

    Intent intent = new Intent();
    intent.putExtra("ParentClassName","A");
    startActivity(intent.setClass(A.this, B.class)); //we are starting activity 'B'

Now in activity B we need to override getSupportParentActivityIntent() and it should look like this,

     @Override
     public Intent getSupportParentActivityIntent() {
           Intent parentIntent= getIntent();

           //getting the parent class name
           String className = parentIntent.getStringExtra("ParentClassName");

           Intent newIntent=null;

           try {
                //you need to define the class with package name
                newIntent = new Intent(B.this,Class.forName("com.myapplication."+className));
          } catch (ClassNotFoundException e) {
                e.printStackTrace();
           }
           return newIntent;
      }

Sunday, February 9, 2014

Hello everyone and Welcome to my blog

I am Chathura De Silva. I had a thought of writing a blog to post about interested and new things that I have learnt during my work (which means developing applications). As I am interested on mobile and web technologies and those areas have high demand in the industry, I spend most of my time to learn them and I use this blog to post the things such as solutions to the problems that I have encountered, methods to implement various things with code samples, new trends/developments in the field, new apps etc. I do not target any particular audience here and I hope to use this blog as a reference to me. But anyone can free to comment to posts or share knowledge which makes a good time for all of us.