Making Tafhim App : Lesson 3 : Project Overview

২৬. SearchArabic

২৭. SearchSelection
২৮. SearchSuggestion_Arabic
২৯. SearchTafsir
৩০. SearchVumika
৩১. Settings2
৩২. SimpleIME (কীবোর্ড)

৩৩. SuggestionSelection
৩৪. TokenActivity (মেইন এ্যাক্টিভিটি)
৩৫. TokenExplShow ( ব্যাখ্যা দেখার এ্যাক্টিভিটি)
৩৬. VumikaAndProsongo
৩৭. VumikaAndProsongoText
৩৮. WebViewActivity (মানচিত্র)
৩৯. WebViewShow (মানচিত্র)
৪০. ZekhanePorchilen (যেখানে পড়ছিলেন)

Theory : 2 : OnCreate, OnStart, OnResume, OnPause, OnStop, OnDestroy, OnRestart - Please Explain The Activity LifeCycle



Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()? What is its purpose?
OK, as my first answer was pretty long I won't extend it further so let's try this...
public DriveToWorkActivity extends Activity
    implements onReachedGroceryStoreListener {
}

public GroceryStoreActivity extends Activity {}
PLEASE NOTE: I've deliberately left out the calls to things like super.onCreate(...) etc. This is pseudo-code so give me some artistic licence here. ;)
The methods for DriveToWorkActivity follow...
protected void onCreate(...) {
    openGarageDoor();
    unlockCarAndGetIn();
    closeCarDoorAndPutOnSeatBelt();
    putKeyInIgnition();
}

protected void onStart() {
    startEngine();
    changeRadioStation();
    switchOnLightsIfNeeded();
    switchOnWipersIfNeeded();
}

protected void onResume() {
    applyFootbrake();
    releaseHandbrake();
    putCarInGear();
    drive();
}

protected void onPause() {
    putCarInNeutral();
    applyHandbrake();
}

protected void onStop() {
    switchEveryThingOff();
    turnOffEngine();
    removeSeatBeltAndGetOutOfCar();
    lockCar();
}

protected void onDestroy() {
    enterOfficeBuilding();
}

protected void onReachedGroceryStore(...) {
    Intent i = new Intent(ACTION_GET_GROCERIES, ...,  this, GroceryStoreActivity.class);
}

protected void onRestart() {
    unlockCarAndGetIn();
    closeDoorAndPutOnSeatBelt();
    putKeyInIgnition();
}
OK, so it's another long one (sorry folks). But here's my explanation...
onResume() is when I start driving and onPause() is when I come to a temporary stop. So I drive then reach a red light so I pause...the light goes green and I resume. Another red light and I pause, then green so I resume. The onPause() -> onResume() -> onPause() -> onResume() loop is a tight one and occurs many times through my journey.
The loop from being stopped back through a restart (preparing to carry on my journey) to starting again is perhaps less common. In one case, I spot the Grocery Store and the GroceryStoreActivityis started (forcing my DriveToWorkActivity to the point of onStop()). When I return from the store, I go through onRestart() and onStart() then resume my journey.
I could put the code that's in onStart() into both onCreate() and onRestart() and not bother to override onStart() at all but the more that needs to be done between onCreate() -> onResume()and onRestart() -> onResume(), the more I'm duplicating things.
So, to requote once more...
Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()?
If you don't override onStart() then this is effectively what happens. Although the onStart()method of Activity will be called implicitly, the effect in your code is effectively onCreate() -> onResume() or onRestart() -> onResume().
=======================

onStart() means Activity entered into visible state and layout is created but can't interact with this activity layout.
Resume() means now you can do interaction with activity layout




Theory : 1 : What is "Context" in Android ?



Simple Example to understand context in android :
Every boss has an assistant to look after, to do all less important and time-consuming tasks. If a file or a cup of coffee is needed, an assistant is on the run. Some bosses barely know what’s going on in the office, so they ask their assistants regarding this too. They do some work themselves but for most other things they need the help of their assistants.
In this scenario,
Boss – is the Android application
Assistant – is context
Files/Cup of coffee – are resources
We generally call context when we need to get information about different parts of our application like Activities, Applications etc.
Some operations(things where the assistant is needed) where context is involved:
  • Loading common resources
  • Creating dynamic views
  • Displaying Toast messages
  • Launching Activities etc.
Different ways of getting context:
getContext()

getBaseContext()

getApplicationContext()

this

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.



Activity diagram





What's Context exactly?
Per the Android reference documentation, it's an entity that represents various environment data. It provides access to local files, databases, class loaders associated to the environment, services (including system-level services), and more. Throughout this book, and in your day-to-day coding with Android, you'll see the Context passed around frequently.
From the "Android in Practice" book, p. 60.
Several Android APIs require a Context as parameter
If you look through the various Android APIs, you’ll notice that many of them take an android.content.Context object as a parameter. You’ll also see that an Activity or a Service is usually used as a Context. This works because both of these classes extend from Context.

An Android Context is an Interface (in the general sense, not in the Java sense; in Java, Context is actually an abstract class!) that allows access to application specific resources and class and information about application environment.
If your android app was a web app, your context would be something similar to ServletContext (I am not making an exact comparison here).
Your activities and services also extend Context, so they inherit all those methods to access the environment information in which the app is running.

Definition of Context

  • Context represents environment data
  • It provides access to things such as databases

Simpler terms (example 1)

  • Consider Person-X is the CEO of a start-up software company.
  • There is a lead architect present in the company, this lead architect does all the work in the company which involves such as database, UI etc.
  • Now the CEO Hires a new Developer.
  • It is the Architect who tells the responsibility of the newly hired person based on the skills of the new person that whether he will work on Database or UI etc.

Simpler terms (example 2)

  • It's like access of android activity to the app's resource.
  • It's similar to when you visit a hotel, you want breakfast, lunch & dinner in the suitable timings, right?
  • There are many other things you like during the time of stay. How do you get these things?
  • You ask the room-service person to bring these things for you.
  • Here the room-service person is the context considering you are the single activity and the hotel to be your app, finally the breakfast, lunch & dinner have to be the resources.

Things that involve context are:
  1. Loading a resource.
  2. Launching a new activity.
  3. Creating views.
  4. obtaining system service.

Context is the base class for ActivityServiceApplication, etc

Another way to describe this: Consider context as remote of a TV & channel's in the television are resources, services, using intents etc - - - Here remote acts as an access to get access to all the different resources into foreground.
  • So, Remote has access to channels such as resources, services, using intents etc ....
  • Likewise ... Whoever has access to remote naturally has access to all the things such as resources, services, using intents etc

Different methods by which you can get context
  • getApplicationContext()
  • getContext()
  • getBaseContext()
  • or this (when in the activity class)

Example:
TextView TV=new TextView(this);
this -> refers to the context of the current activity.