Android program to switch modes. Switching between applications in Android. Use adb to backup software on your computer

The Launcher tab, which opens from the Home screen, contains icons for all applications installed on your phone, including applications downloaded and installed from Android Market or other sources.

When you open an app, other apps you're using don't stop; they continue running, playing music, displaying web pages, and more. You can quickly switch between apps to work with multiple apps at once. operating room Android system and the applications running on it communicate so that unused applications do not consume resources. Applications are stopped and started again when necessary. Therefore, there is no reason to stop applications unless you are sure that the downloaded application is not functioning correctly. For details about how applications use memory and how to manage those applications, see .

Learn about finding and installing additional apps for your phone (including performance tools, utilities, games, reference materials and other types software) see section.

Opening and closing the Launchpad

If you have more apps open than can fit on the launcher, you can scroll the launcher up or down to see all the apps.

You can add an app icon to your Home screen by pressing and holding the icon on the Launcher until it vibrates, and then dragging the icon to the desired location on the Home screen.

The Launchpad closes automatically when you tap an icon to open an app or move an icon from the Launchpad to the Home screen.

Opening an application

  • Click the application icon in the Launcher panel.
  • Tap the app icon on the home screen.

Switch to a recently used app

A small window will open containing icons of all recently used applications.

  • Tap an icon to open its associated application.

Or click the button Back to return to the current application.

I'm making an application that needs to be able to switch between applications that the user has open (for example, applications in the multitasking menu), I have the ComponentName of 10 new applications, and can start them (i.e., switch to them) like this:

Intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setComponent(info); startActivity(intent);

However, when I try to change on Facebook (And probably some similar apps), it asks for a specific permission:

java.lang.SecurityException: Permission Denial: starting Intent ( flg=0x10000000 cmp=com.facebook.katana/.activity.FbFragmentChromeActivity ) from ProcessRecord(42310bc0 8578:com.javanut13.multitaskheads/u0a10095) (pid=8578, uid=10095 ) requires com.facebook.permission.prod.FB_APP_COMMUNICATION

Is there a way around this by actually switching to the app instead of starting it? Can I just send the focus to a different application?

0

1 answers

If you don't have permission, I think you can only start another application's activity when its "export" attribute is set to true. A Facebook-like application might set this to false and does not allow the other application access to its activity.

I haven't tested this alone, but you can try the following codes:

Intent extApp= getPackageManager().getLaunchIntentForPackage(facebook-package-name); this.startActivity(extApp);

Hope this helps.

An application does not always consist of one screen. For example, we have created very useful program and the user wants to know who its author is. He clicks on the “About” button and is taken to new screen, where there is useful information about the program version, author, website address, how many cats the author has, etc. Think of the activity screen as a web page with a link to another page. If you look at the code in the file MainActivity.java from previous lessons, you will see that our class MainActivity also applies to Activity(or his heirs) or, more precisely, inherited from him.

Public class MainActivity extends AppCompatActivity

As you might guess, we should create a new class that could be similar to MainActivity and then somehow switch to it when the button is pressed.

For the experiment, we will take the program from the first lesson and use a button for experiments (or create new project with one button on the screen). Next, let's create a new form to display useful information. For example, let's show the user what a cat does when it goes left and right. Agree, this is very important information that provides the key to unraveling the Universe.

We will create a new activity manually, although the studio has ready-made templates. But there is nothing complicated there and for a better understanding it is useful to do everything by hand.

Let's create a new XML markup file activity_about.xml in folder res/layout. Right click on the folder layout and select from context menu New | Layout resource file. A dialog box will appear. In the first field, enter the file name activity_about. In the second, you need to enter the root element. By default it's there ConstraintLayout. Erase the text and enter ScrollView. Entering a few characters is enough for the studio to suggest ready-made options; you can immediately press Enter without waiting for the full word to be entered:

You will get a corresponding blank into which we will insert the element TextView.

Information will be retrieved from resources, namely the string resource about_text. Now it is highlighted in red, signaling the absence of information. It was possible to press Alt+Enter and enter text in the dialog box. But for our example, this method will not work, since our text will be multi-line, using control characters. So let's do it differently. Let's open the file res/values/strings.xml and enter the following text manually:

There is a green oak near the Lukomorye;\n A golden chain on that oak:\n Both day and night scientist cat\nEverything goes around and around in a chain;\nIt goes right- the song starts,\n Left- he tells a fairy tale.

We used the simplest HTML text formatting tags like , , . For our example, it is enough to highlight in bold the words that relate to the cat and the direction of movement. To move text to a new line, use symbols \n. Let's add another string resource for the title of the new screen:

About the program

We figured out the markings. Next you need to create a class for the window AboutActivity.java. Select from the menu File | New | Java Class and fill in the required fields. At first, it is enough to indicate only the name. Then you'll deal with other fields.

Let's get the blank.

Now the class is almost empty. Let's add the code manually. The class must inherit from an abstract class Activity or his relatives like FragmentActivity, AppCompatActivity etc. Let's add extends Activity. The activity class must have a method onCreate(). Place the mouse cursor inside the class and select from the menu Code | Override Methods(Ctrl+O). In the dialog box we look for the required class; you can type the first characters on the keyboard for a quick search. In the created method you need to call the method setContentView(), which will load the prepared markup onto the screen. We will have this option.

Package ru.alexanderklimov.helloworld; import android.app.Activity; import android.os.Bundle; /** * Created by Alexander Klimov on 12/01/2014. */ public class AboutActivity extends Activity ( @Override protected void onCreate(Bundle savedInstanceState) ( super.onCreate(savedInstanceState); setContentView(R.layout.activity_about); ) )

Now comes the most important part. Our task is to go to a new screen when we click a button on the first screen. Let's go back to class MainActivity. Let's write a button click handler:

Public void onClick(View view) ( Intent intent = new Intent(MainActivity.this, AboutActivity.class); startActivity(intent); )

Here I used the button click processing method described in the lesson.

To launch a new screen you need to create an instance of the class Intent and indicate the current class in the first parameter, and the class to go to in the second, we have this AboutActivity. After this the method is called startActivity(), which launches a new screen.

If you now try to test the application in the emulator, you will receive an error message. What did we do wrong? We missed one important step. You need to register a new one Activity in the manifesto AndroidManifest.xml. Find this file in your project and double click on it. The file editing window will open. Add a new tag after the closing tag for the first activity. Type on your own and actively use the prompts. You will get the following:

This is where the string resource comes in handy about_title. We launch the application, click on the button and get a window About the program. Thus, we learned how to create a new window and call it by clicking a button. And we have a mega-convenient program at our disposal - now we will always have at hand a hint of what the cat does when it goes left.

Once again, please note that the second activity class created must inherit from the class Activity or similar ones ( ListActivity etc.), have an XML markup file (if required) and be specified in the manifest.

After calling the method startActivity() a new activity will be launched (in this case AboutActivity), it will become visible and move to the top of the stack containing the running components. When calling a method finish() from a new activity (or when the hardware return key is pressed) it will be closed and removed from the stack. The developer can also navigate to the previous (or any other) activity using the same method startActivity().

Creating a third screen - a method for the lazy

Programmers, like cats, are lazy creatures. Always remember that for an activity you need to create markup and a class that inherits from Activity, and then don’t forget to register the class in the manifest - oh well.

In this case, select from the menu File | New | Activity | Basic Activity(or other template). Next, the familiar window for creating a new activity will appear. Fill in the required fields.

Click on the button Finish and the activity will be ready. To verify this, open the manifest file and check for a new entry. I’m not even talking about class and markup files, they will appear in front of you on their own.

Add a new button to the main activity screen yourself and write code to switch to the created activity.

At first, I would advise you to manually create all the necessary components for the new activity so that you understand the relationship between the class, markup and manifest. And when you get the hang of it, you can use the Activity Creation Wizard to speed up your work.

Passing data between activities

We used a simple example to call another activity screen. Sometimes you not only need to call a new screen, but also transfer data to it. For example, username. In this case, you need to use a special area extraData, which the class has Intent.

Region extraData is a list of pairs key/value, which is transmitted along with the intention. Strings are used as keys, and any primitive data types, arrays of primitives, class objects can be used for values Bundle and etc.

To transfer data to another activity, use the method putExtra():

Intent.putExtra("Key", "Value");

The receiving activity must call some appropriate method: getIntExtra(), getStringExtra() etc.:

Int count = getIntent().getIntExtra("name", 0);

Let's redo the previous example. We already have three activities. The first activity will have two text fields and a button. Appearance could be as follows:

At the second activity SecondActivity set the element TextView, in which we will display the text received from the first activity. Let's write the following code for the method onCreate() at the second activity.

@Override protected void onCreate(Bundle savedInstanceState) ( super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); String user = "Animal"; String gift = "donut hole"; TextView infoTextView = (TextView)findViewById( R.id.textViewInfo); infoTextView.setText(user + " , you were given " + gift); )

If we now run the program and simply call up the second window, as was described in the first part of the article, then we will see the default inscription Animal, they handed you a donut hole. Agree, it’s quite annoying to receive such messages.

Let's correct the situation. Add the code to the first activity:

Public void onClick(View view) ( EditText userEditText = (EditText) findViewById(R.id.editTextUser); EditText giftEditText = (EditText) findViewById(R.id.editTextGift); Intent intent = new Intent(MainActivity.this, SecondActivity. class); // into the username key we push the text from the first text field intent.putExtra("username", userEditText.getText().toString()); // into the gift key we push the text from the second text field intent.putExtra("gift ", giftEditText.getText().toString()); startActivity(intent); )

We placed the object in a special container Intent two keys with values ​​that are taken from text fields. When the user enters data into the text fields, it will go into this container and be passed to the second activity.

The second activity should be ready to warmly receive messages as follows (in bold).

// Default values ​​String user = "Animal"; String gift = "donut hole"; user = getIntent().getExtras().getString("username"); gift = getIntent().getExtras().getString("gift"); TextView infoTextView = (TextView)findViewById(R.id.textViewInfo); infoTextView.setText(user + " , you were given " + gift);

Now the message looks less offensive, and even pleasant for some. In complex examples, it is advisable to add a check when processing data. There may be situations where you launch a second activity with empty data of type null, which may cause the application to crash.

In our case, we know that we are expecting a string value, so the code can be rewritten like this:

Intent intent = getIntent(); user = intent.getStringExtra("username");

User = getIntent().getStringExtra("username");

The program has a drawback - it is not clear from whom we receive greetings. Any well-bred monkey will not accept a gift from an anonymous source. So as homework, add another text field to enter the name of the user who is sending the message.

Google recommends using the following format for keys: your package name as a prefix, followed by the key itself. In this case, you can be sure of the uniqueness of the key when interacting with other applications. Something like this:

Public final static String USER = "ru.alexanderklimov.myapp.USER";

Who framed the cat Vaska - we get the result back

It's not always enough to simply pass data to another activity. Sometimes you need to get information back from another activity when it is closed. If earlier we used the method startActivity(Intent intent), then there is a related method startActivityForResult(Intent intent, int RequestCode). The difference between the methods is additional parameter RequestCode. It's basically just an integer number that you can come up with yourself. It is needed in order to distinguish from whom the result came. Let's say you have five additional screens and you assign values ​​from 1 to 5 to them, and using this code you can determine whose result you need to process. You can use the value -1, then it will be the same as calling the method startActivity(), i.e. we won't get any results.

If you use the method startActivityForResult(), then you need to override the method in your code to receive the result onActivityResult() and process the result. Confused? Let's look at an example.

Let's say you're a detective. Information was received that two pieces of sausage and other products were stolen from the table of an influential person in a restaurant. Suspicion fell on three suspects - a crow, a fucking dog and Vaska the cat.

One of the visitors provided a series of photos from his show-off iPhone:


There is also testimony from another witness: And Vaska listens and eats.

Create a new project Sherlock with two activities. On the first screen there will be a button to switch to the second screen and a text label in which the thief's name will be displayed.


Top