What is AdvanceTech ?

For some viewers below image for The Sun set and for some viewers The Sun rises , For me this is Rise of hope ! With this attitude I would like to share my knowledge and experience in this site. This is my first time to write any blog  😉 .

I would like to thanks my wife to encourage me to write blogs .

post

Advertisement

AIDL use case!

AIDL does nothing but lets the system to generate the boilerplate code that hides the binder IPC details, so that you can invoke the remote service API as a local method call. Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. So,If you don’t need IPC (i.e., your client and server stay in the same process), you don’t need AIDL;

If you want to write the boilerplate code yourself for IPC, you don’t need AIDL;

If your service is not complicated enough (i.e., does not require concurrent multithreaded accesses), you can use system provided Messenger API for IPC. You don’t need your own AIDL, because the Messenger API hides the AIDL usage;

To extend the case 3, if you can use any existing lib or existing API to access a service in another process, you don’t need your own AIDL. For example, you can access ActivityManagerService with existing system API, and all the AIDL stuff for IActivityManager is hidden by the system API.

Use AIDL or Messenger Queue?

AIDL is for the purpose when you’ve to go application level communication for data and control sharing, a scenario depicting it can be: An app requires list of all contacts from Contacts app (content part lies here) plus it also wants to show the call’s duration and you can also disconnect it from that app (control part lies here).

In Messenger Queues you’re more IN the application and working on threads and processes to manage the queue having messages so no Outside services interference here.

Messenger is needed if you want to bind a remote service (e.g. running in another process).

How would you save Activity state during a screen rotation?

When your orientation changes, you don’t have to manually change to the landscape layout file. Android does this automatically for you. When orientation changes, Android destroys your current activity and creates a new activity again, this is why you are losing the text.

Basically, whenever Android destroys and recreates your Activity for orientation change, it calls onSaveInstanceState() before destroying and calls onCreate() after recreating. Whatever you save in the bundle in onSaveInstanceState, you can get back from the onCreate() parameter.

private TextView mTextView;
private static final String KEY_TEXT_VALUE = "textValue";
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mTextView = (TextView) findViewById(R.id.main);
  
  if (savedInstanceState != null) {
      CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
      mTextView.setText(savedText);
  }
}

@Override
protected void onSaveInstanceState (Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putCharSequence(KEY_TEXT_VALUE, mTextView.getText());
}

When you choose Fragment over Activity?

  • Activities are designed to represent a single screen of my application, while
  • Fragments are designed to be reusable UI layouts with logic embedded inside of them

Google advises you to always use Fragments. In the simplest case, Fragments are used like containers of activities. Android 4 (ICS) supports both Smartphones and Tablets. This means the same application will be running on a smartphone and a tablet and they are likely to be very different.


Historically each screen in an Android app was implemented as a separate Activity. This creates a challenge in passing information between screens because the Android Intent mechanism does not allow passing a reference type (i.e. object) directly between Activities. By making each screen a separate Fragment, this data passing headache is completely avoided. Fragments always exist within the context of a given Activity and can always access that Activity. By storing the information of interest within the Activity, the Fragment for each screen can simply access the object reference through the Activity.

How to pass data between Activities in Android?

Consider a scenario after login activity , Sign-out button visible in each activity , now we need to keep session ID available of all activities !!

Solution:

The easiest way to do this would be to pass the session id to the signout activity in the Intent you’re using to
start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

What is ConstraintLayout in Android?

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It’s similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it’s more flexible than RelativeLayout and easier to use with Android Studio’s Layout Editor.

Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

How fragment get context in Android?

You can use getActivity() , which returns the activity associated with a fragment. The activity is a context (since
Activity extends Context ). You can also override the onAttach() method of fragment:

public static class DummySectionFragment extends Fragment{
...
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    DBHelper = new DatabaseHelper(activity);
}
}

What is Context on Android ?

The Context class is an Interface to global information about an application environment.

We may assume a Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. An Android app has activities. Context is like a handle to the environment your application is currently running in. The activity object inherits the Context object.

Type of Context in android!!

The are mainly two types of context:

  • Application Context: It is an instance that is the singleton and can be accessed in activity via getApplicationContext() . This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of activity.
  • Activity Context: This context is tied to the lifecycle of an activity. The activity context should be used when you are passing the context in the scope of an activity or you need the context whose lifecycle is attached to the current context.