What is the use of Android static inner activity class and where exactly we can use? - android

I have created a Activity called MainActivity Which extends from ThirdActivity.
Next, I have an inner static activity called AnotherActivity which extends from SecondActivity. Below is my code:
1)MainActivity: which is the first activity that calls at first when app loads.
public class MainActivity extends ThirdActivity {
public static class AnotherActivity extends SecondActivity{
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Main Activity");
}
}
2) ThirdActivty Code follows:
public abstract class ThirdActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
System.out.println("Third Activity");
}
}
3) Inner static activity extended from SecondActivity which consists of:
public abstract class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
System.out.println("Second Activity");
}
}
Now the output of the above android program is:
Third Activity
MainActivity
In order to access the inner activities we need to do declare in manifest as:
<activity android:name="MainActivity$AnotherActivity" ></activity>
So, My question goes like this:
1) How to access Static inner Activity classes.
2) When to use this scenario.?
3) Will the inner activity runs the life cycle process and what happens to already running MainActivity?

I love answering bizarre questions before having coffee.
1) How to access Static inner Activity classes
What do you mean by "access"? The fact that this is a static inner class is pretty much irrelevant. It will run like an normal Activity. When it is running, its onCreate method, e.g., will be called. When that happens, there is a reference to the running instance of AnotherActivity in the canonical variable this.
2) When to use this scenario.
I can't think of a single reason. In fact, I can think of a million reasons not to use it. Ever.
3) Will the inner activity runs the life cycle process and what
happens to already running MainActivity?
Yes. The "inner" activity will run through a normal Activity lifecycle. Nothing happens to the MainActivity because it is not running when the AnotherActivity is running. When AnotherActivity is started, MainActivity is stopped (onPause, maybe onStop, etc).

Related

How to prevent the Screentshot in the entire android app without repeating the same code

Hi everyone i want to block the Screenshot in my app. I got the first problem solve from here.
But now the thing is I have more than 10 activity and 10 + fragment.
Is there any way to do this just by writing in the one class and giving it reference to the entire app.
Just like we make one Application class and in the AndroidMainfest.xml give that application class refrence.
You can implement a BaseActivity, and make all your activities extend this BaseActivity. In onCreate() of this activity set the flag. You need to ensure all your activities call super.onCreate() as follows:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your flag here
...
}
}
Activity1.java
public class Activity1 extends BaseActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
}

Getting activity context without instantiating it?

I have been struggling with this problem for two days,I am in situation where i need to use a method in ActivityB from ActivityA . The problems lays in getting the context of A i have tried many solutions like:
static ActivityA activityA;
In onCreate state:
activityA = this;
and add this method:
public static ActivityA getInstance(){
return activityA;
}
In ActivityB, call
ActivityA.getInstance().myFunction(); //call myFunction using activityA
it did not work out because this need the ActivityA to be instantiated in order to pass its context to A but this is not accomplishable in my case is there any way of getting an activity's context without switching activities .
my question might turn out to be simple or intuitive but im new to this concept , thanks in advance
As you want to have common functionality in both activities, you can create BaseActivity that extends Activity and define your method in that and extend ActivityA and ActivityB by BaseActivity then you can access methods.
You can do it like this,
public class BaseActivity extends Activity
{
public void myFunction()
{
...
}
}
And do this for other activities:
public class ActivityA extends BaseActivity
{
public void someMethod()
{
myFunction(); // you can call function here directly
}
}
You could extent class A using Class B simply
OR
public static ActivityA activityA;
In onCreate state:
{
activityA = this;
}
Outside Oncreate
public myFunction{
}
and in ActivityB call
activityA.myFunction();
Here I Created Two Classes Consider as Activities , And Then Created one Public methodA() in class Activity_A , then Created Class Activity_B and Created methodB() , And Created Object of Activity_A and Called methodA() by passing context of Activity Activity_B .
class Activity_A{
public void methodA(Context context){
Toast.makeText(context,"methodA",Toast.LENGTH_LONG).show();
}
}
class Activity_B{
public void methodB(){
Activity_A activity_a = new Activity_A();
activity_a.methodA(Activity_B.this);
}
}
There are two options:
1) Add the static keyword to your shared methods
OR
2) You can try reflection.
For reference follow the link:
What is reflection and why is it useful?

Android app: How to remove a view displayed by an Activity?

First I have a method which creates an activity which displays a view:
Intent intent = new Intent(viewController, TestActivity.class);
viewController.TestActivity(intent);
My TestActivity class calls setContentView(layout) and displays a view.
This works fine, my problem is trying to remove the Intent/View from the screen. I know I can call destroy() from within my Activity, however I am trying to remove the view from outside of the Activity class. I want the class which created the Activity/Intent to be able to remove it as well.
Any ideas? It seems like this should be a trivial fix however I'm unable to find the solution online for some reason. Thanks!
Make a sigleton-like pattern to get the instance of the started activity.
public class TestActivity extends Activity{
private static TestActivity instance;
public static TestActivity getInstance(){
return instance;
}
public void onCreate(Bundle savedInstanceState){
//xxxxx
super.onCreate(savedInstanceState);
instance = this;
}
protected void onDestroy(){
//xxxxx
super.onDestroy();
instance = null;
}
public void finishOutSide(){
this.finish();
}
}
Then in the outside,use these code to finish the activity:
if(TestActivity.getInstance() != null){
TestActivity.getInstance().finishOutSide();
}
All of your Activity instances can be destroyed when the sustem is running low on memory. Hence, it causes unexpected behaviour. Finish an Activity itside itself only or in a foreground Service that cannote be unexpectedly killed.
You have to rethink your application architecture to avoid such issue.

Android - execute code onResume and onPause for all the activities of the app?

I'm designing an architecture where the app needs to execute certain set of operations everytime it goes to background (onPause) and a set of operations everytime it comes back to foreground (onResume), irrespective of the activity (for all the activities). Is there a way with which I can achieve this, without having to call those methods in every activity class' onPause and onResume overrides?
Make your own class that extends Activity and add your desired behavior to its onPause and onResume methods.
Then you extend that class on your activities.
public class BaseActivity extends Activity {
#Override
protected void onPause() {
// ...
}
#Override
protected void onResume() {
// ...
}
}
public class Activity1 extends BaseActivity {
// ...
}
You could extends your Activities by a BaseActivity which extends Activity, and create the two methods onPause / onResume in it.
See this answer for more information.

Call method from other class

Ok I am a complete newb when it comes to java classes. I have a public method that dynamically displays some Linearlayouts with some stuff in them. For instance this method (public void methodA)is in ClassA.java, then I want to call methodA from inside ClassB.java. Both of the classes extend Activity and the methodA is being called in the OnCreate method.
ClassA.java
public class ClassA extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodA();
}
public void methodA() {
//Do Stuff
/* This uses:
* Package Manager
* Buttons using(this)
* Linear Layouts using(this)
* TextViews using(this)
* findViewById()
* startActivity
*/
}
}
ClassB.java
public class ClassB extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodA(); //How do I do this
}
}
It is clear to me that the method is specific to the first activity, which means that it shouldn't be called in the second one. You can either reimplement the method in the second activity or, if these activities are similar (don't do this if they aren't!), you have two options:
Inherit the second activity from the first one.
Merge these two activities into one and use different intents to launch them and act accordingly.
The second method is easier to maintain, so I would prefer it over the first one in simpler cases.
Craete instance of class A in class B and then you can invoke MethodA from class B
public class ClassB extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//MethodA(); //How do I do this
ClassA a=new ClassA ();
a.MethodA();
}
}

Categories

Resources